Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print a file to paper in Python 3 on windows XP/7?

For a project i'm working on, I need to be able to write to a text file and then send that file to the printer. I'm fairly new to programming (1.5 yrs) and in my time have never needed to do this.

So, my question is how would I send the file to the printer? (Obviously I know how to write to the file in the first place). I need the program to work on windows XP and windows 7, I've come across something called the lpr online but I can't find anything substantial, and this seems to be related to UNIX based OS'es.

I have a feeling I may need to use os.system() somewhere but am unsure how I would go about this.

like image 686
DoubleTapThat Avatar asked Nov 25 '11 16:11

DoubleTapThat


2 Answers

A simple approach is to have Notepad print the text file:

import subprocess
subprocess.call(['notepad', '/p', filename])
like image 70
Eryk Sun Avatar answered Nov 20 '22 06:11

Eryk Sun


I did this a long time ago, so I don't remember the specifics, but it involved going through a win32 extension for python to call the Windows API related to printing.

You may also consider options such as generating a pdf file.

You may find the following links useful:

  • http://timgolden.me.uk/python/win32_how_do_i/print.html
  • http://newcenturycomputers.net/projects/pythonicwindowsprinting.html
  • http://www.blog.pythonlibrary.org/2010/02/14/python-windows-and-printers/
like image 36
Jong Bor Lee Avatar answered Nov 20 '22 08:11

Jong Bor Lee