Discussion:
[Tutor] Printing txt files in landscape from python
János Juhász
2007-02-01 16:00:18 UTC
Permalink
Hi All,

do you have any idea, how I can send a txt file to the default printer in
landscape view with python on windows.
I wanted to set up just the char size and the orientation of the printout.

thinking about
os.system('notepad.exe /pt "%%%s"' % filename)


Yours sincerely,
______________________________
János Juhász
Christopher Arndt
2007-02-01 16:40:24 UTC
Permalink
Post by János Juhász
do you have any idea, how I can send a txt file to the default printer in
landscape view with python on windows.
I assume that by "txt file", you mean a file containing ASCII text?
Post by János Juhász
I wanted to set up just the char size and the orientation of the printout.
Printers normally don't understand ASCII file sent to them, unless you
configure them (with some status codes) to do so.

Normally, the OS converts a text file sent to its printing system to
something the printer understands, like PostScript or PL5/6, and allows
you to set other options, e.g. setting landscape mode or choosing the
paper tray. Under windows, this is what the printer drivers are for,
under MAC OS X and Linux, this is done by the CUPS system.

Unfortunately, the specifics depend highly on the system, the printer
driver, the printer and the application that sends the file to the print
system.
Post by János Juhász
thinking about
os.system('notepad.exe /pt "%%%s"' % filename)
So this is actually your safest bet, but will only work under windows
obviously. Under Linux, you could try to use the 'a2ps' programm, but it
is not installed everywhere.

Chris
Tim Golden
2007-02-01 17:13:20 UTC
Permalink
Post by János Juhász
Hi All,
do you have any idea, how I can send a txt file to the default printer in
landscape view with python on windows.
I wanted to set up just the char size and the orientation of the printout.
thinking about
os.system('notepad.exe /pt "%%%s"' % filename)
Doesn't completely answer your question, but
have a look at this:

http://timgolden.me.uk/python/win32_how_do_i/print.html

and perhaps consider a ReportLab solution. It's ridiculously
difficult to set up the printing params construct under
Windows (just search for DEVMODE) so might well be easier
to use a PDF approach.

TJG
Terry Carroll
2007-02-02 02:14:17 UTC
Permalink
Post by János Juhász
do you have any idea, how I can send a txt file to the default printer in
landscape view with python on windows.
I wanted to set up just the char size and the orientation of the printout.
I've gotten a crush on wxPython, now that it's nicely documented in the
"wxPython in Action" book.

Take a look at http://wiki.wxpython.org/index.cgi/Printing for a
discussion of printing.

Here's an example on printing, copying the code from "Code Sample - Easy
Printing" on that page.


###################################

# this part copied from URL above:

from wx.html import HtmlEasyPrinting

class Printer(HtmlEasyPrinting):
def __init__(self):
HtmlEasyPrinting.__init__(self)

def GetHtmlText(self,text):
"Simple conversion of text. Use a more powerful version"
html_text = text.replace('\n\n','<P>')
html_text = text.replace('\n', '<BR>')
return html_text

def Print(self, text, doc_name):
self.SetHeader(doc_name)
self.PrintText(self.GetHtmlText(text),doc_name)

def PreviewText(self, text, doc_name):
self.SetHeader(doc_name)
HtmlEasyPrinting.PreviewText(self, self.GetHtmlText(text))

# now, using it:

text_to_print = """
Congress shall make no law respecting an establishment of religion,
or prohibiting the free exercise thereof; or abridging the freedom
of speech, or of the press; or the right of the people peaceably to
assemble, and to petition the government for a redress of
grievances.
"""

app = wx.PySimpleApp()
p = Printer()
p.Print(text_to_print, "Amend 1")

###################################


This works, and gives you (well, the user) the option of printing
landscape.

I'm not sure how to go about specifying a font. I suspect you'll have to
go with the more heavyweight "Code Sample - `(wx)Printout` Printing"
examplefor that.

Loading...