Lolivier
Lolivier

Reputation: 51

How to use win32print in an OpenERP module?

I try to use a Bixolon receipt printer with OE on Windows 7. I success to print directly from a small python module using win32print (coming with py32win) with the code below :

win32print is not natively in OE so I paste win32print.pyd in OE server directory and put the code in a wizard of my OE module. I can see my wizard, launch it without error but then nothing happens : no print, no error message.

Any ideas ? Thank you

import win32print

printer=OpenPrinter(win32print.GetDefaultPrinter())
hJob = win32print.StartDocPrinter (printer, 1, ("RVGI Print", None, "RAW"))
g=open('test3.txt','r')
raw_data = bytes ( open( 'test3.txt' , 'r').read ())
try:
    win32print.StartPagePrinter (printer)
    win32print.WritePrinter (printer, raw_data)
    win32print.EndPagePrinter (printer)
finally:
    win32print.EndDocPrinter (printer)
win32print.ClosePrinter (printer)

Upvotes: 2

Views: 995

Answers (2)

marcelocra
marcelocra

Reputation: 2503

Well, I don't know if you typed here incorrectly, but the way you imported the win32print module force you to attach it to module function calls and you haven't done this in your first line:

printer = OpenPrinter(win32print.GetDefaultPrinter())

should be

printer = win32print.OpenPrinter(win32print.GetDefaultPrinter())

And there is another point that I don't really understands, but Tim Golden put in his tutorial and could be your problem:

raw_data = bytes('your_text')

could be just:

raw_data = 'your_text'

depending on your system version. You also haven't specified the encoding, but since you got no errors that may not be the problem.

For the version thing Tim Golden posted:

import sys
if sys.version_info >= (3,):
  raw_data = bytes ("This is a test", "utf-8")
else:
  raw_data = "This is a test"

Hope it helps!

Upvotes: 0

Daniel Reis
Daniel Reis

Reputation: 13342

Remember that the python code runs on the server. Is your printer connected to the server?

Also, you don't have an except section in your try. That makes errors go by silently. Try removing the try block so that errors are raised. Looking at them you might figure out the issue.

Upvotes: 1

Related Questions