Send print job to USB printer using Python

I can start with a PDF, PRN or PS file. How to send it to a USB printer using Python? Which module should I start working with?

+6
source share
3 answers

You seem to be using Windows, so start with this - the answer will change if you are using Linux.

There are two ways to print in Windows. The first most common way is to send individual drawing commands through the Windows GDI. To do this, you must place each individual element on the page in the right place (text lines, images and shapes) when choosing the right colors and fonts. It is easy if you generate the data yourself, it is much more difficult if you need to analyze the file you are reading.

Another option is to send to the printer in "raw" mode, when the printer driver will cost a lot. To do this, the printer must naturally understand the byte stream that you feed it. There are some printers that understand Postscript from the beginning, but I'm not sure about PDF, and PRN is not a standard format.

I have never done rough printing through Python itself, but here is a link to a short piece of sample code (and the idea of ​​the expected problems): http://bytes.com/topic/python/answers/512143-printing-raw-postscript-data-windows

+2
source

As far as I know, these are two available packages:

+1
source
import wx import win32api import win32print class ComboBoxFrame(wx.Frame): def __init__(self): # creates a drop down with the list of printers available wx.Frame.__init__(self, None, -1, 'Printers', size=(350, 300)) panel = wx.Panel(self, -1) list=[] #Enum printers returns the list of printers available in the network printers = win32print.EnumPrinters( win32print.PRINTER_ENUM_CONNECTIONS + win32print.PRINTER_ENUM_LOCAL) for i in printers: list.append(i[2]) sampleList = list wx.StaticText(panel, -1, "Please select one printer from the list of printers to print:", (15, 15)) self.combo =wx.ComboBox(panel, -1, "printers", (15, 40), wx.DefaultSize,sampleList, wx.CB_READONLY ) btn2 = wx.Button(panel, label="Print", pos=(15, 60)) btn2.Bind(wx.EVT_BUTTON, self.Onmsgbox) self.Centre() self.Show() def Onmsgbox(self, event): filename='duplicate.docx' # here the user selected printer value will be given as input #print(win32print.GetDefaultPrinter ()) win32api.ShellExecute ( 0, "printto", filename, '"%s"' % self.combo.GetValue(), ".", 0 ) print(self.combo.GetValue()) if __name__ =='__main__': app = wx.App() ComboBoxFrame().Show() app.MainLoop() 
0
source

Source: https://habr.com/ru/post/890373/


All Articles