I'm trying to write simple PyQT report editor and what I need is to exactly know what sizes the page has, MS-Word like.
Today I found the following link:
How to access QTextDocument pages
that shows how to use an QPrinterPreviewDialog, and it seems to be the answer to my question, but I don't know how to transform it to Python. I have no idea how to implement QPrinterPreviewDialog into a QTextEdit. I tried to read the docs but without a example it's very hard to start.
Related
I am wondering if and how it is possible to 'listen to' the text that is in my browser window.
I am specifically NOT looking to scrape websites in the sense that I want to crawl them for information, I am just interested in interacting with an arbitrary page that makes my browser output text.
Example
Suppose I am asking a question on Stack Overflow.
By the time I type a title of 'Listen to text in browser' the suggestions appear, one of them contains plain text 'Listen to browser request'
As soon as the word 'request' is on my screen, the browser will get shut down and I order a pizza
What would a good solution look like
I want to be able to do this for practically any website that somehow makes my computer show simple text. Ideally without having knowledge of how the text is generated.
I want this to be somewhat fast, subsecond should be possible
I do not want to hit the website or its api's, just want to use the information that is already on my screen.
I am not too picky about the OS and browser requirements.
I can also imagine there may be corner cases wher it is hard (perhaps text is shown as a picture, or perhaps parts of a sentence are actually spread across multiple textboxes that are just displayed to eachother). For now I just wonder how this can be done for a simple page.
Bonus points if it could even capture text from the field in which I am typing myself, so I can scald myself when I am about to say something stupid.
What have I come up with so far
I am in general confident that I can process the text once I get it into a tool, however the main challenge is on how one can listen to the browser.
I tried looking at the source code, but that does not appear to contain this dynamic text
Perhaps there is a steraming API on the browser itself, that can stream out changes?
Perhaps there is a way to grab all text from a browser, perhaps 10x per second or so
Using the normal scraping solutions are completely not what I want, so I do not want to fire a request to the webserver 10x per second.
In the worst case, I suppose we could use screen capture software, followed by text recognition, but I really hope there is something more elegant
I suppose there may be automation/testing software that can do this. That would be an answer but something lightweight (e.g. a python library) would be nicest.
I have tried searching but did not find any solution, or even the question. Presumably I am using the wrong words.
I am trying to set up a print job in Python under Linux.
Now there is this GtkUnixPrintDialog that basically does what I need it to do, the problem is that I can't find any documentation on how to deal with it. I tried looking at the GTK3 reference docs, but that's all for C, and trying to translate those commands to Python didn't work.
I can easily get the dialog to open by adding it to my .glade file - but I can't find a way in Glade to connect the appropriate signals to it. Nor can I find a way to display it like other dialogs.
Anyone with a working example, to do a print job using the dialog?
Based on my previous question Creating print job in gtk3/python I have the code to set up the page as it has to be.
I am using reportlab libraries to create a pdf file, and that pdf file has to be printed out, preferably with a little control on which printer to use. Of course I could bypass that dialog entirely and dump it on the default printer, but the lack of documentation on that dialog irritates me as well.
It looks like GtkPrintUnixDialog is not included in introspection sources for GTK+. I am not sure if this is an oversight or done on purpose. However, this dialog is used internally by the higher level printing interface anyway which can be invoked with Gtk.PrintOperation.run by passing Gtk.PrintOperationAction.PRINT_DIALOG. There is a complete example in the pygobject sources.
I'm using python docx which claims in the documentation that:
'Often, a picture is placed in a paragraph by itself, but this is not required. It can have text before and after it in the paragraph in which it’s placed.'
But I cant find out how to do this, could someone explain (idealy with a basic example) how I get text before the image while in the same paragraph please. So the line of text ends with an image.
I've not found any answers to this but have seen people asking the same elsewhere with no solution.
Thanks
(note: I'm not a hugely experiance programmer and other than this awkward part the rest of my code will very basic)
At the time of this writing, python-docx doesn't have the features to support what you're trying to do.
The feature that would support it would be Run.add_picture(). If you add a feature request to the python-docx issue tracker, I'll see how soon we can get to it.
In the meantime, if you wanted to dig in and see what you could hack up, I'd recommend starting here, at Document.add_picture, as the structure would be analogous and use mostly the same calls.
If you just want to write docx files with Python, you can use another module:
https://github.com/rafaels88/py2docx
Python noobie.
I'm trying to make Python select a portion of my screen. In this case, it is a small window within a Firefox window -- it's Firebug source code. And then, once it has selected the right area, control-A to select all and then control-C to copy. If I could figure this out then I would just do the same thing and paste all of the copies into a .txt file.
I don't really know where to begin -- are there libraries for this kind of thing? Is it even possible?
I would look into PyQt or PySide which are Python wrapper on TOp of Qt.
Qt is a big monster but it's very well documented and i'm sure it will help you further in your project once you grabbed your screen section.
As you've mentioned in the comments, the data is all in the HTML to start (I'm guessing it's greyed out in your Firebug screenshot since it's a hidden element). This approach avoids the complexity of trying to automate a browser. Here's a rough outline of how I would get the data:
Download the HTML for the whole page - I'd do this manually at first (i.e. File > Save from a browser), and if there are a bunch of pages you want to process, figure out how to download all the pages you want later. If you want to use python for this part, I'd recommend urllib2. The URLs for each page are probably pretty structured, so you could easily store them in a list, and download each one and save it locally. .
Write a script to parse the HTML - don't use regex. Since you're using Python, use something like Beautiful Soup, which will create a nice object representation of the page, and then you can get the elements you want.
You mention you're new to python, so there's definitely going to be a learning curve around this, but this actually sounds like a pretty doable project to use to learn some more python.
If you run into specific obstacles with each step, start a new question with a bit of sample code, showing what you're trying to accomplish, and people will be more than willing to help out.
I have a Python3 GUI, where user selects certain values to be statistically evaluated and/or plotted (hist).
Out of this GUI on users request I want to create a report, prefferebly in DOC or/and ODT or/and PDF formats. The layout of the report is pretty much fixed, what will change is: the names of the selected measures and the corresponding values (there are always three measures to display - user selects which), matplotlib histogram (will change depending on the selected settings), user name, date etc.
In some older thread I found a reference to pod package which looks very close to my needs. But I didn't find any good illustration of the resulting documents. And what is most important, I'm not sure if images are allowed there - I would imagine reserving a place for the image somewhere in the corner of the document and update it when user request the report.
EDIT1: pod does NOT support Python3, also after 2to3 convertion and couple of small fixes. There was an advice to use LaTeX, but I'm not sure I understand how to implement this idea without going to the very low level of coding.
Any suggestions are greatly appreciated since the rest of the project is nearly done and this is the last big unsolved problem.
EDIT2: After some break I return to this problem. Since my GUI is in PyQt4, I finally decided to go with QTextDocument and print document to PDF. There are some unclear moments for me in this approach, like if I should use QTextCursor and programically create the document or create somehow HTML first. Also, I have to figure out how to create the document without displaying editor window. Any references with examples are welcome. So far I found only couple of examples in PyQt4\examples\richtext, but they don't answer all my questions.
I try to look at ReportLab now - it doesn't provide DOC (only PDF), but seems to be flexible. As I learned from FAQ, images are allowed, but I will have to save them from matplotlib in jpeg first (PIL is not available for Python3 yet). I hope I will not end up with extreme low level programming for report creation...
Finally solved the problem. It was not so difficult at the end, and I ended up using QTextDocument and its setHtml method to produce the desired document. Related question with the code is here