How to print a txt file in Python

6

How to print a txt file in Python?

I'm not referring to print but rather to print to the printer.

Thank you (:

    
asked by Gabriel Azevedo 20.05.2015 в 21:03
source

2 answers

6

Python is a generic programming language - and has no relation to how the operating system provides its peripherals or printing APIs for program use.

In Windows, these calls typically involve the use of win32api - a Python module with extensions that allow direct access to Windows API32 - here is a page with an example of how to do this, passing directly the name of the file you want to print. To install Win32API, download the version corresponding to your Python from the last build (higher number) here: link

This is true for stand-alone scripts. If you are using a multiplatform library for desktop applications such as GTK + or Qt - this library usually has calls to print - they will usually include calls to open the print dialog, which displays the available printers, and mount your document to memory, and then move on to the printing api - in both cases it's worth looking at the documentation in C, why unfortunately the documentation in Python is a bit flawed - Python calls are mapped more or less directly if you are already programming in one of two frameworks:

For Qt4 you can see here: link E for gtk +: link - (It seems that the composition of the document you need to do is even more complex.)

On MacOSX / Linux, if you're not using GTK + or Qt, you're going to have to render your document to postscript (Tkinter can do this straight if your application is with it - otherwise, my suggestion is to install and use "enscript" as an external program to render your text as a "silly" postscript file.

After that you can launch the printing using CUPS - the suggestion is to use PyCUPS to be able to choose the printer programmatically and send your postscript file as a job for it.

As you can see, it may not be an easy task - especially if you want a cross-platform solution. An interesting hack I wrote down here, and maybe the most interesting thing for you is: use Python to open the file in the default System Browser - and ask the user to use the browser's print menu for printing. The vanatem of this method is that it is easy to compose the document with HTML and CSS instead of pure text, and to have a very beautiful impression:

import webbrowser
webbrowser.open("file:///<caminho>/<para>/<seu-arquivo>")
print ("O documento a ser impresso foi aberto no navegador - use
as opções de impressão do mesmo")

PS. if it is a dot-matrix printer that accepts direct text, without passing through drivers, on any system, just copy the file to the printer's device file:

import shutil
shutil.copy(<seu_arquivo>, "prn")  # "prn" no windows - no linux, tipicamente /dev/lp0

update another way to create a decent text output, which you can export to postscript, and then copy that postscript to the printer device (Mac, linux) or use ghostscript to pass of Postscript for a specific printer that does not support postscript directly is to use Cairo (PyCairo) - this library has several features for handling text and characters - supporting different fonts, colors, etc. ...

Unfortunately, the thing is still tricky - you would probably have to code the logic of wrapping lines manually. Using Cairo would play the role of the external "enscript" program I mentioned above, but giving you all the control of rendering.

    
answered by 20.05.2015 / 23:26
source
3

To this day I preferred to generate in PDF using Reportlab. It is not the most direct solution, but at least it is the most compatible. If you want to automate printing, just invoke Adobe Reader from the command line, in the case of Windows. If I'm only in * nix environment, I'd only be with PS anyway.

    
answered by 21.05.2015 в 19:38