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)
win32print.StartDoc(pDC, ('desktop.bmp', None, None, 0)) win32print.StartPage(pDC) win32gui.StretchBlt(pDC, 0, 0, int(printerwidth * .9), int(printerheight * .9), pcDC, 0, 0, printerwidth, printerheight, win32con.SRCCOPY) font = win32gui.LOGFONT() font.lfHeight = int(printerheight / 20) font.lfWidth = font.lfHeight font.lfWeight = 150 font.lfItalic = 1 font.lfUnderline = 1 hf = win32gui.CreateFontIndirect(font) win32gui.SelectObject(pDC, hf) win32gui.SetBkMode(pDC, win32con.TRANSPARENT) win32gui.SetTextColor(pDC, win32api.RGB(0, 255, 0)) win32gui.DrawText( pDC, 'Printed by Python!', -1, (0, 0, int(printerwidth * .9), int(printerheight * .9)), win32con.DT_RIGHT | win32con.DT_BOTTOM | win32con.DT_SINGLELINE) win32print.EndPage(pDC) win32print.EndDoc(pDC) win32print.ClosePrinter(p) win32gui.DeleteObject(dcBM) win32gui.DeleteObject(pcBM) win32gui.DeleteObject(hf) win32gui.DeleteDC(dDC) win32gui.DeleteDC(dcDC) win32gui.DeleteDC(pDC) win32gui.DeleteDC(pcDC)