> I thought I'd put a page break, chr(12), character in a txt file I wrote
> to skip to the top of the page. It doesn't work. Comments?
>
Did you intend to ask a question? If so you might like to read
http://catb.org/~esr/faqs/smart-questions.html first.
Yes, it does work.
--
Grant Edwards grante Yow! ... bleakness
at ... desolation ... plastic
visi.com forks ...
But are you sending the raw control codes to the printer, or are
you sending the image-of-my-text-document to a printer-GDI which
then renders the as-you-see-it out of the printer?
The pseudo-pipeline comparison would be
type file.txt > lpt1:
which would send the raw text file to the printer (assuming it's
set up on LPT1, otherwise, use whatever port it's attached to in
your printer control panel); or are you using something like
notepad file.txt
File -> Print
which renders to an internal image representation and then sends
that image out to the printer. If it were a dot-matrix printer,
you'd here/see the difference in a jiffy -- the raw dump is fast
and uses the printer's built-in fonts while the render-as-image
is slow and NOISY.
One alternative is possibly to set up the "Generic Text" printer
as a device type and attach it to the same port; I've had fair
fortune with this letting me control the printer more directly if
I want fast dumps (particularly on dot-matrix printers) rather
than pretty dumps.
-tkc
It's not a matter of you controlling the GDI stuff. Unless
you're writing directly to the printer device, printing on
Windows is done (whether by Notepad, gvim, Word, Excel, whatever)
into a graphical representation which is then shipped off to the
printer. So if you're printing from Notepad, it's going to print
what you see (the little square), because Notepad renders to this
graphical representation to print. If you send the file
*directly* to the printer device (bypassing the Win32 printing
layer), it will send the ^L directly and should eject a new page
on most printers.
-tkc
Have you considered the possibility that your printer can't print
raw text files? I had one that would ONLY print Postscript. Embedding
a chr(12) would accomplish nothing, you HAD to use a driver that
would translate chr(12) into the appropriate Postcript codes.
What you're doing MIGHT work for others with different printers.
> I am writing a txt file. It's up to the user to print it using Notepad
> or some other tool.
WordPad will interpret chr(12) as you want.
Neil
I suppose I could copy the txt file into wordpad, and print it there.
> I thought I'd put a page break, chr(12), character in a txt file I wrote
> to skip to the top of the page. It doesn't work. Comments?
The 1970's are over, and neither Notepad nor your printer attempts to
maintain compatibility with a Teletype model 37.
That may be the solution. Just tell the end user to copy the file into
it, and print it there.
I just tried it in Wordpad, and it works, but my --- underlines are
pushed together. Maybe tabs instead of spaces. The columns past Seq # in
WordPad may suffer from the characters not being fixed width. Well, a
little work with WordPad might be enough for users to get it right.
"Copy txt file into wordpad. Select all the text, and set format to
fixed (if that's possible.).
Here's what a txt sample looks like. It has line wrap here, and the page
feed
Date/Time & Station UTC Seq # Frames Time Span Pix
Dst Pix/Sec
------------------- -- ------------------- ----- ------ ---------
------- -------
2008/11/12 17:38:58 WW 2008/11/13 01:38:58 1 Noise data. Short
track.
2008/11/12 17:39:24 WW 2008/11/13 01:39:24 2 Noise data. Short
track.
<-------------PAGE FEED
Date/Time & Station UTC Seq # Frames Time Span Pix
Dst Pix/Sec
------------------- -- ------------------- ----- ------ ---------
------- -------
2008/11/17 22:29:54 WW 2008/11/18 06:29:54 21 Noise data. Short
track.
2008/11/18 01:51:36 WW 2008/11/18 09:51:36 22 Noise data. Short
track.
2008/11/18 04:05:03 WW 2008/11/18 12:05:03 23 Noise data. Short
track.
2008/11/18 17:40:42 WW 2008/11/19 01:40:42 24 95 3.17
48.17 15.21
Why not generate a PostScript or PDF file in the first place? Check
out reportlab.
--
D'Arcy J.M. Cain <da...@druid.net> | Democracy is three wolves
http://www.druid.net/darcy/ | and a sheep voting on
+1 416 425 1212 (DoD#0082) (eNTP) | what's for dinner.
New Courier and NotePad produces a good looking result.
I'm trying to keep this effort to a minimum. I don't think tracking down
how to write PP code PDF code is worth for this effort.
In another related effort that I might get involved in, it would be good
to be able to produce graphical data as from MatPlotLib and be able to
print that to a printer directly from a Python program.
If you're writing it to a text file and assuming that Notepad is
smart enough to properly handle form-feeds, I'm sorry, you'll be
disappointed...this says more about the brain-deadness of Notepad
than your optimism :-)
If you have a configurable destination, you might be able to do
something within your Python app like
if 'win' in sys.platform.lower():
default_dest = "lpt1:"
else:
default_dest = "/dev/lp0"
dest = config.get("printer", default_dest)
f = file(dest, 'wb')
f.write(my_output_with_ff)
# optionally
# f.write(chr(12))
# to eject the last page
f.close()
Otherwise, you'd have to write to something a default Windows
application would know how to handle with embedded
form-feeds/page-breaks (i.e., not Notepad as the default .txt
handler). My first thought would be to export it as RTF (there
was a good python RTF library I tinkered with one afternoon --
it's a quick google away) which should allow embedding
page-breaks, and even give you a fair bit of additional control
over other aspects like fonts and styles.
-tkc
How about creating a new print job for each discontinuous page? AFAIK,
modern printer spooler don't continue printing between separate jobs? A
bit of hackery, but guaranteed to work unless you're using roll paper.
In another response, Tim Chase suggested creating an RTF file instead
of plain text. I think this is your best bet if your goal is to get
page breaks with the least amount of additional effort.
The package he's probably referring to is PyRTF. I took a quick look
at it and in my opinion it's overkill for your purposes. Since RTF is
actually just a markup language, and the only features of it that you
absolutely need are (1) a way to ensure a fixed-width font is used and
(2) a way to insert page breaks, it's probably quickest and simplest
to just throw the markup into the document yourself (well, with
Python) and naming the result with the .rtf suffix instead of .txt.
So, how do you find out what markup to use? Open WordPad, select the
font you want, type a bit of text, save the file as RTF (this should
be the default), and open up the file in Notepad. At the top will be
a bunch of setup codes, including the font selection. Make your
Python program put that whole spiel (including a trailing space or
newline, to separate your text from the last RTF tag) at the top of
your output. There will be one unmatched curly brace, which you
should close at the end of the document. On my computer, it looks
like this:
{\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fmodern
\fprq1\fcharset0 Courier New;}}
{\*\generator Msftedit 5.41.15.1515;}\viewkind4\uc1\pard\f0\fs20
That takes care of the font (and other stuff, but you are interested
in the font). The only other things you need to do are put
r'\par' (the end-paragraph tag) at the end of each line and r'\page'
at the end of each page. Again, remember to make sure these tags
don't collide with your actual text. (I'd use '\\par\n' in place of
'\n' and '\\page\n' in place of chr(12).) Finally, make sure to end
with a closing curly brace as previously mentioned.
The RTF solution is probably more robust than embedding chr(12) and
telling the user to use WordPad, especially since I saw someone report
on another forum that chr(12) doesn't always work even in WordPad (it
might be dependent on the version of WordPad, or the printer, or
whatever).
I am too lazy to have actually read any documentation on RTF, but it's
freely available on the Web should you need to reference it.
John
Odd that TXT files under Windows still use the same 13 (carriage return), 10
(linefeed) and 9 (tab) codes that used to work on my ASR 33. So why not code
12 (formfeed)?
--
Bartc
Yes, John is correct -- it was PyRTF that I tinkered with (which
can be overkill if all you want to do is what John describes).
I'm not well versed in RTF, and he demonstrates:
> I am too lazy to have actually read any documentation on RTF, but it's
> freely available on the Web should you need to reference i
even in his laziness, he helpfully provided a quick guide on how
to hand-create simple documents without the addition of a
full-blown RTF toolkit like PyRTF.
-tkc
>> In another related effort that I might get involved in, it would be good
>> to be able to produce graphical data as from MatPlotLib and be able to
>> print that to a printer directly from a Python program.
>
A client of mine just built a web site where output from queries could
be HTML, PDF (Piza and ReportLab) or CSV (just written out). It's not
that hard (though the code isn't open sourced I may be able to get them
to document an outline of the process, or let me (in my copious spare time).
regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
PyCon is coming! Atlanta, Feb 2010 http://us.pycon.org/
Holden Web LLC http://www.holdenweb.com/
UPCOMING EVENTS: http://holdenweb.eventbrite.com/
My point was these other control codes from over 30 years ago are still in
use today, so why not also make use of chr(12)?
--
Bartc
FF still works with a lot of things. Most ASCII printers still
honor it, as do pagers and programs like a2ps. I still use it
in ASCII text files...
--
Grant
> FF still works with a lot of things. Most ASCII printers still
> honor it, as do pagers and programs like a2ps. I still use it
> in ASCII text files...
Yes. It's especially useful for making large sectional structure in a
text file — for example, program source code — that can then be easily
navigated “by page” with a text editor like Emacs or Vim.
Since it's a standard white space character by definition, it can go
anywhere that white space can go; and yet it acts to signal structure
within the file.
--
\ “There's a certain part of the contented majority who love |
`\ anybody who is worth a billion dollars.” —John Kenneth |
_o__) Galbraith, 1992-05-23 |
Ben Finney
Until recently at least printers were non-text-only but did not necessarily
accept only bitmap images. For example they might use Postscript, HP/PCL or
Epson Esc/p (or whatever the exact names were). I'm fairly certain the last
two responded directly to chr(12) codes in the input stream.
Windows may or may not drive these in bitmap mode (which can be simpler for
Windows, but then you lose any special processing available inside the
printer), but given the task of sending a TXT file to such a printer, it is
not unreasonable to expect it to honour codes such as CR, LF and FF,
especially as most printers seem to be page-oriented now rather than use
roll or fanfold paper.
It would just need to translate FF into the page-break command for the
printer in question, eg. send the image so far and eject the page, just as
it might do for EOF.
--
bartc