Ejemplo n.º 1
0
def cfgPrt(selPrt):

    p = win32print.OpenPrinter(selPrt)
    printprocessor = win32print.GetPrinter(p, 2)['pPrintProcessor']
    dmsize = win32print.DocumentProperties(0, p, selPrt, None, None, 0)
    driverextra = dmsize - pywintypes.DEVMODEType().Size
    dm = pywintypes.DEVMODEType(driverextra)
    dm.Fields = dm.Fields | win32con.DM_ORIENTATION | win32con.DM_COPIES  #|win32con.DM_PAPERSIZE
    dm.Orientation = win32con.DMORIENT_LANDSCAPE
    dm.Copies = 1
    #dm.Papersize=win32con.DM_PAPERLENGTH(790)
    #dm.Paperwidth=530
    win32print.DocumentProperties(
        0, p, selPrt, dm, dm, win32con.DM_IN_BUFFER | win32con.DM_OUT_BUFFER)
    hDC = win32gui.CreateDC(printprocessor, selPrt, dm)
    printerwidth = win32ui.GetDeviceCaps(hDC, 110)
    printerheight = win32ui.GetDeviceCaps(hDC, 111)
Ejemplo n.º 2
0
def showPrtSetting(selPrt):
    from ctypes import windll

    PRINTER_DEFAULTS = {"DesiredAccess": win32print.PRINTER_ALL_ACCESS}
    pHandle = win32print.OpenPrinter(selPrt, PRINTER_DEFAULTS)

    #To show Document property setting
    win32print.DocumentProperties(0, pHandle, selPrt, None, None, 5)
Ejemplo n.º 3
0
def prepPrtTray():
    
    # Constants from wingdi.h
    DM_OUT_BUFFER = 0x02
    DM_IN_BUFFER = 0x08
    DM_IN_PROMPT = 0x04
    DM_DEFAULT_SOURCE = 0x200
    
    # Get a handle for the default printer
    device_name = win32print.GetDefaultPrinter()
    handle = win32print.OpenPrinter(device_name)
    
    # Get the default properties for the printer
    properties = win32print.GetPrinter(handle, 2)
    devmode = properties['pDevMode']
    
    # Print the default paper source (prints '7' for 'Automatically select')
    print(devmode.DefaultSource)
    
    # Change the default paper source to '4' for 'Manual feed'
    devmode.DefaultSource = 4
    devmode.Fields = devmode.Fields | DM_DEFAULT_SOURCE
    
    # Write these changes back to the printer
    win32print.DocumentProperties(None, handle, device_name, devmode, devmode, DM_IN_BUFFER | DM_OUT_BUFFER)
    
    # Confirm the changes were updated
    print(devmode.DefaultSource)  # Aaargh! Prints '7' again!
    
    # Start printing with the device
    hdc = win32gui.CreateDC('', device_name, devmode)
    win32print.StartDoc(hdc, ('Test', None, None, 0))
    win32print.StartPage(hdc)
    
    # ... GDI drawing commands ...
    
    win32print.EndPage(hdc)
    win32print.EndDoc(hdc)
Ejemplo n.º 4
0
import win32print, pywintypes, win32con, win32gui, win32api

pname = win32print.GetDefaultPrinter()
print(pname)
p = win32print.OpenPrinter(pname)
print('Printer handle: ', p)
print_processor = win32print.GetPrinter(p, 2)['pPrintProcessor']
## call with last parm set to 0 to get total size needed for printer's DEVMODE
dmsize = win32print.DocumentProperties(0, p, pname, None, None, 0)
## dmDriverExtra should be total size - fixed size
driverextra = dmsize - pywintypes.DEVMODEType(
).Size  ## need a better way to get DEVMODE.dmSize
dm = pywintypes.DEVMODEType(driverextra)
dm.Fields = dm.Fields | win32con.DM_ORIENTATION | win32con.DM_COPIES
dm.Orientation = win32con.DMORIENT_LANDSCAPE
dm.Copies = 2
win32print.DocumentProperties(0, p, pname, dm, dm,
                              win32con.DM_IN_BUFFER | win32con.DM_OUT_BUFFER)

pDC = win32gui.CreateDC(print_processor, pname, dm)
printerwidth = win32print.GetDeviceCaps(pDC, win32con.PHYSICALWIDTH)
printerheight = win32print.GetDeviceCaps(pDC, win32con.PHYSICALHEIGHT)

hwnd = win32gui.GetDesktopWindow()
l, t, r, b = win32gui.GetWindowRect(hwnd)
desktopheight = b - t
desktopwidth = r - l
dDC = win32gui.GetWindowDC(hwnd)

dcDC = win32gui.CreateCompatibleDC(dDC)
dcBM = win32gui.CreateCompatibleBitmap(dDC, desktopwidth, desktopheight)
Ejemplo n.º 5
0
    def printOut(self, file_name):

        # List available printers
        #print("Available printers")
        #print(win32print.EnumPrinters(0,"None",1))
        #print(win32print.EnumPrinters(1,"None",2))
        #print(win32print.EnumPrinters(3,"None",1)[4])
        #print(win32print.EnumPrinters(3,"None",5))

        # Use Default Printer
        #printer_name = win32print.GetDefaultPrinter ()
        printer_name = "MITSUBISHI CP70D Series(USB)"
        print("Printer: " + printer_name)

        hprinter = win32print.OpenPrinter(printer_name, {"DesiredAccess": win32print.PRINTER_ALL_ACCESS})
        devmode = win32print.GetPrinter(hprinter, 2)["pDevMode"]

        # DEVMODE
        devmodeSize=win32print.DocumentProperties(0, hprinter, printer_name, None, None, 0)
        devmode = pywintypes.DEVMODEType(devmodeSize - pywintypes.DEVMODEType().Size)
        #devmode.Fields = devmode.Fields|win32con.DM_ORIENTATION|win32con.DM_COPIES

        win32print.DocumentProperties(0, hprinter, printer_name, devmode, devmode, 0)

        '''
        try:
            win32print.SetPrinter(hprinter, 2, properties, 0)
        except pywintypes.error, err:
            print(err[2])
            #sys.exit()
        '''

        gDC = win32gui.CreateDC("WINSPOOL", printer_name, devmode)

        hDC = win32ui.CreateDCFromHandle(gDC)

        self.printable_area = hDC.GetDeviceCaps (self.HORZRES), hDC.GetDeviceCaps (self.VERTRES)
        print "printable_area",self.printable_area

        printer_size = hDC.GetDeviceCaps (self.PHYSICALWIDTH), hDC.GetDeviceCaps (self.PHYSICALHEIGHT)
        print "printer_size",printer_size

        printer_margins = hDC.GetDeviceCaps (self.PHYSICALOFFSETX), hDC.GetDeviceCaps (self.PHYSICALOFFSETY)
        print "printer_margins",printer_margins
        
        bmp = Image.open (file_name)

        print "bmp.size[0]",bmp.size[0]," bmp.size[1]",bmp.size[1]

        #if bmp.size[0] > bmp.size[1]:
          #bmp = bmp.rotate (90)

        ratios = [1.0 * self.printable_area[0] / bmp.size[0], 1.0 * self.printable_area[1] / bmp.size[1]]
        print "ratios",ratios

        scale = min (ratios)
        print "scale",scale
        
        hDC.StartDoc (file_name)
        hDC.StartPage () 

        dib = ImageWin.Dib (bmp)
        scaled_width, scaled_height = [int (scale * i) for i in bmp.size]

        print "scaled width",scaled_width," scaled height",scaled_height


        x1 = int ((printer_size[0] - scaled_width) / 2)
        y1 = int ((printer_size[1] - scaled_height) / 2)
        x2 = x1 + scaled_width
        y2 = y1 + scaled_height

        print "x1, y1, x2, y2",x1, y1, x2, y2

        #dib.draw (hDC.GetHandleOutput (), (x1-5, y1+38, x2-25, y2-24))
        #dib.draw (hDC.GetHandleOutput (), (x1-5, y1+38, x2, y2))
        dib.draw (hDC.GetHandleOutput (), (0, 0, scaled_width, scaled_height))

        hDC.EndPage ()
        hDC.EndDoc ()
        hDC.DeleteDC ()
Ejemplo n.º 6
0
DM_DEFAULT_SOURCE = 0x200

# Get a printer for the default printer
device_name = win32print.GetDefaultPrinter()
printer = win32print.OpenPrinter(device_name)

# Get the default properties for the printer
properties = win32print.GetPrinter(printer, 2)
devmode = properties['pDevMode']

# Change the default paper source to '4' for 'Manual feed'
devmode.DefaultSource = 1
devmode.Fields = devmode.Fields | DM_DEFAULT_SOURCE

# Write these changes back to the printer
win32print.DocumentProperties(None, printer, device_name, devmode, devmode, DM_IN_BUFFER | DM_OUT_BUFFER)

file_name = 'Hello World'.encode()

# Start printing with the device
hdc = win32gui.CreateDC('', device_name, devmode)


hJob = win32print.StartDocPrinter (printer, 1, ("RVGI Print", None, "RAW"))
g=open('Test.txt','r')
raw_data = (open(r'Test.txt').read()).encode()
try:
    win32print.StartPagePrinter (printer)
    win32print.WritePrinter (printer, raw_data)
    win32print.EndPagePrinter (printer)
finally: