Print my wx.Panel after formatting it - python

I am working on an app using wxPython and it kind of needs to print the info shown to the user. On click of the print button, The data shown in the current panel has to be formatted a bit and then be sent to the printer queue.
Google searches give me ways to print text documents. How do I go about with this?

wxPython provides a couple of printing options. You can check out the full printing framework it provides here:
http://wxpython.org/Phoenix/docs/html/printing_framework_overview.html
http://wxpython.org/Phoenix/docs/html/Printout.html
It's fairly complex, but it is also the most flexible way to print using wxPython. They do have another option though that's called HtmlEasyPrinting:
http://wxpython.org/Phoenix/docs/html/html.HtmlEasyPrinting.html
You might want to take a look at that if you're new to wxPython. Of course, you could also use the reportlab package to generate a nicely formatted PDF of your text and then send that to the printer or just open it in the user's default PDF viewer so they can print it.

Related

Print contents of WebView in Python (Gtk3.0)

I have designed a very simple Python3 app that uses a WebView to render Markdown files.
Now I'd like to add the possibility to print the rendered file (with the current stylesheet that's being applied). I'm desperately trying to find some information about how to do it. Is there any tutorial or howto out there that could explain how to do it?
I've found a few examples explaining how to print PDF files from disk, but I'd like to print things directly from the WebView. Apparently there is even a Method for that but I'm failing miserably on figuring out on how to use it...
See here
Best Regards
Merlin
OK,
I finally figured it out. It appears that only the Webkit2 library is modern enough to handle printing from a Webview. I was using WebKit1 and that was the reason it wouldn't work. So I had to change my dependencies and after that it was actually quite easy...
Lets say that you have a Webview whose name is myWebView, then your function to print it's content would look like:
def onPrintButtonClicked( self, widget ):
#Set the operation up
printOperation = WebKit2.PrintOperation.new( self.myWebView )
#Run the dialog
printOperation.run_dialog()
After the dialog, you could actually check if the user has chosen to print or to cancel by checking for WebKit2.PrintOperationResponse.CANCEL or WebKit2.PrintOperationResponse.PRINT return values.
Reagards

Making clickable text in Python [duplicate]

I'm working on a console application. My application uses urwid lib. In some cases, I need to show very long hyperlinks as short text inside table columns. I want to open links in the browser when I click on the text inside the column.
So, my question is:
It is possible to print text as a hyperlink to the console?
Can you provide a small example of how to print text as a hyperlink using python?
A bit late, but there totally is a way to do that now, without curses or anything, just pure text and collaboration from your terminal emulator.
def link(uri, label=None):
if label is None:
label = uri
parameters = ''
# OSC 8 ; params ; URI ST <name> OSC 8 ;; ST
escape_mask = '\033]8;{};{}\033\\{}\033]8;;\033\\'
return escape_mask.format(parameters, uri, label)
Call that function with link('https://example.com/') to get a simple, clickable link, or link('https://example.com/', 'example') for a custom label.
Note that links are faintly underlined in your terminal, and not all of them support the feature.
Yes, using some tools, like gNewt or Curses, you could create a button and 'on click' do an action (like open a browser to a given url).
gNewt : http://gnewt.sourceforge.net/
nCurses : https://docs.python.org/3.7/library/curses.html
Otherwise, it's the terminal application that will manage the text you give it, and if it doesn't implement uri's recognition your program won't work as you'd like.
No, some consoles do recognize urls and convert them to a clickable hyperlink. All you can do is make it easy to recognize for console applications by putting a http:// in your url.
Also see
How does bash recognize a link?

How to type into a text field in Windows Explorer?

I am trying to upload a file to a website, and when I click on the upload button (using module WebBot) it opens Windows Explorer. Am I able to output the name of the file into the File Name field? I have the full path of the file, I just need to get the actual text into the File Name box.
I'd consider two approaches here:
Use a python library specifically for interaction with the Windows GUI. I've had good experiences with Pywinauto once, seems still pretty usable at first glance. Hook this in when you expect the explorer window to open. Code may conceptually look like this - do some test run and print all available handles from the upload dialog (just guessing here as a hint, see Pywindocs):
app = Application().connect(title_re=".*Upload file", path=r"c:\windows\explorer.exe")
dlg = app.window(title_re=".*Upload file", path=r"c:\windows\explorer.exe")
app.dlg.print_control_identifiers()
Check if you could simply do a POST or similar with the corresponding data. This is a very vague alternative as you do not provide information about what to upload and what the underlying backend/concept of the website is, but in the simplest case this may even be a more elegant option. A quick search brought up this short and simple example for this: https://stackoverflow.com/a/43942648/10192615

How to create a file browser in wxpython

I am developing the GUI for my application using wxpython and have most of the features down, except in the main frame/window I want to have a box for choosing a file (in this case, the input will have to be an excel file). Something similar to the standard filebrowser that is accessed whenever you choose "open" from a menu.
Below is an image to show exactly what I want...
You probably want a wx.FileDialog. It provides access to the default file dialog of the OS your app is running in. You can see an example of how it's used in the wxPython demo package. This tutorial also has some screenshots and sample code:
http://www.blog.pythonlibrary.org/2010/06/26/the-dialogs-of-wxpython-part-1-of-2/
The screenshot you show appears to be an interface to actually open the dialog. You can easily create that using sizers and basic widgets. Then just bind the open button to a handler that will show the dialog.
You might also want to take a look at the FileBrowseButton from wx.lib.filebrowsebutton (also in the demo).
There are a few other related widgets which you might be interested in too: wx.DirDialog, MultiDirDialog or wx.GenericDirDialog.
Assuming you know the basics of wxPython you can use wx.GenericDirCtrl and wx.ListCtrl to make nice browser

Reportlab printing defaults

I am using reportlab to create a pdf with some portait and some landscape pages. The pdf looks great on screen, but when printing it the default print settings are to shrink the pages, rather than rotate them. At first I though that this was just something to do with the settings, but a few other people have commented on it, using a variety of pdf readers and printers. After a bit of investigating it seems that this is something to do with an option set inside the pdf itself, recommending those print settings. Does anyone know of a way to change this when the pdf is generated?

Categories