How to print a txt file in Python

Asked

Viewed 4,646 times

5

How to print a txt file in Python?

I am not referring to print but to print on the printer.

Thanks (:

2 answers

6


Python is a generic programming language - and has no relation to how the operating system makes its peripherals or print Apis available for use in programs.

In Windows, these calls typically involve the use of win32api - a Python module with extensions that allow direct access to Windows API32 - here an example page of how to do this, passing the name of the file you want to print. To install Win32api, download the version corresponding to your Python from the latest build (larger number) here: http://sourceforge.net/projects/pywin32/files/pywin32/

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

For Qt4 you can see here: http://doc.qt.io/qt-4.8/printing.html And for gtk+ : http://doc.qt.io/qt-4.8/printing.html - (it seems that the composition of the document you are asked to do is even more complex).


Under Macosx /Linux, if you are not using GTK+ or Qt, you will have to render your document to Postscript (Tkinter can do this directly if your application is the same - otherwise my suggestion is to install and use "enscript" as an external program to compose your text as a "silly" printing arch.

From this you can trigger the printing using CUPS - the suggestion is to use Pycups to choose the printer programmatically and send your Postscript file as a job to it.


As you can see, it may not be an easy task - especially if you want a cross-platform solution. An interesting hack that I leave noted here, and maybe the most interesting for you is: use Python to open the file in the system’s default 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 plain text, and have a very beautiful print:

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 matrix printer, which accepts direct text, without going through drivers, on any system, just copy the file to the printer arqivo-device:

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 move from Postscript to a specific printer that does not support Postscript directly is to use Cairo (Pycairo) - this library has several features for text and character handling - supporting different fonts, colors, etc....

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

  • 1

    So - the whole problem is that - it’s actually not simple. The simple examples are the ones that are there. : -) - after answering I even went to take a look at Tkinter: it doesn’t have a practical method of generating Postscript from text in a Text widget (but it can do it from a Canvas) - the direct implication of that is that without using something like enscripten, or a PDF generator like Reportlab, for a modern printer, your code has to create the output layout of the text.

  • 1

    I went to look, with a reasonable calm, in Pypi if there was a package that abstracts the issue of printers and/or the generation of text with layout (and was cross-platform - but this would be opitional)- but there is no.

  • Related answer - with a few more examples: https://answall.com/questions/280033/automatizar-impress%C3%A3o-de-files-in-a-folder/280043#280043

3

Until today 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, simply invoke Adobe Reader from the command line in the case of Windows. If I am only in *Nix environment, I would only be with PS same.

  • It is - but given my answer, it may even be one of the best ways. (I particularly liked the hack with "webbrowser" - maybe you can put a javascript that triggers the print, along with the text)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.