Kit
Kit

Reputation: 31503

Send a print job to USB printer using Python

I can start with a PDF, PRN, or PS file. How do I send it to a USB printer using Python? What module should I get started with?

Upvotes: 7

Views: 22481

Answers (3)

vamsi
vamsi

Reputation: 1

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()

Upvotes: 0

Mark Ransom
Mark Ransom

Reputation: 308111

It sounds like you're using Windows, so let's start with that - the answer changes if you're using Linux.

There are two ways to print within Windows. The first most common way is to send individual drawing commands through the Windows GDI interface. To do this you must place every individual element on the page in its proper location (text strings, images and shapes) while selecting the proper colors and fonts. Easy if you're generating the data yourself, much harder if you have to parse a file that you're reading.

The other option is to send to the printer in a "raw" mode, where the printer driver essentially gets bypassed. For this to work the printer must natively understand the stream of bytes that you feed to it. There are some printers that understand Postscript natively, but I'm not sure about PDF, and PRN isn't a standard format.

I've never done raw printing through Python myself, but here's a link to a short snippet of sample code (and an idea of the problems to expect): http://bytes.com/topic/python/answers/512143-printing-raw-postscript-data-windows

Upvotes: 2

zengr
zengr

Reputation: 38899

As far as I know, these are the two package available:

  1. pkipplib
  2. win32print

Upvotes: 2

Related Questions