print_desktop.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import win32print, pywintypes, win32con, win32gui, win32api
  2. pname=win32print.GetDefaultPrinter()
  3. print(pname)
  4. p=win32print.OpenPrinter(pname)
  5. print('Printer handle: ',p)
  6. print_processor=win32print.GetPrinter(p,2)['pPrintProcessor']
  7. ## call with last parm set to 0 to get total size needed for printer's DEVMODE
  8. dmsize=win32print.DocumentProperties(0, p, pname, None, None, 0)
  9. ## dmDriverExtra should be total size - fixed size
  10. driverextra=dmsize - pywintypes.DEVMODEType().Size ## need a better way to get DEVMODE.dmSize
  11. dm=pywintypes.DEVMODEType(driverextra)
  12. dm.Fields=dm.Fields|win32con.DM_ORIENTATION|win32con.DM_COPIES
  13. dm.Orientation=win32con.DMORIENT_LANDSCAPE
  14. dm.Copies=2
  15. win32print.DocumentProperties(0, p, pname, dm, dm, win32con.DM_IN_BUFFER|win32con.DM_OUT_BUFFER)
  16. pDC=win32gui.CreateDC(print_processor,pname,dm)
  17. printerwidth=win32print.GetDeviceCaps(pDC, win32con.PHYSICALWIDTH)
  18. printerheight=win32print.GetDeviceCaps(pDC, win32con.PHYSICALHEIGHT)
  19. hwnd=win32gui.GetDesktopWindow()
  20. l,t,r,b=win32gui.GetWindowRect(hwnd)
  21. desktopheight=b-t
  22. desktopwidth=r-l
  23. dDC = win32gui.GetWindowDC(hwnd)
  24. dcDC=win32gui.CreateCompatibleDC(dDC)
  25. dcBM = win32gui.CreateCompatibleBitmap(dDC, desktopwidth, desktopheight);
  26. win32gui.SelectObject(dcDC, dcBM)
  27. win32gui.StretchBlt(dcDC, 0, 0, desktopwidth, desktopheight, dDC, 0, 0, desktopwidth, desktopheight, win32con.SRCCOPY)
  28. pcDC=win32gui.CreateCompatibleDC(pDC)
  29. pcBM=win32gui.CreateCompatibleBitmap(pDC, printerwidth, printerheight)
  30. win32gui.SelectObject(pcDC, pcBM)
  31. win32gui.StretchBlt(pcDC, 0, 0, printerwidth, printerheight, dcDC, 0, 0, desktopwidth, desktopheight, win32con.SRCCOPY)
  32. win32print.StartDoc(pDC,('desktop.bmp',None,None,0))
  33. win32print.StartPage(pDC)
  34. win32gui.StretchBlt(pDC, 0, 0, int(printerwidth*.9), int(printerheight*.9), pcDC, 0, 0, printerwidth, printerheight, win32con.SRCCOPY)
  35. font=win32gui.LOGFONT()
  36. font.lfHeight=int(printerheight/20)
  37. font.lfWidth=font.lfHeight
  38. font.lfWeight=150
  39. font.lfItalic=1
  40. font.lfUnderline=1
  41. hf=win32gui.CreateFontIndirect(font)
  42. win32gui.SelectObject(pDC,hf)
  43. win32gui.SetBkMode(pDC, win32con.TRANSPARENT)
  44. win32gui.SetTextColor(pDC,win32api.RGB(0,255,0))
  45. win32gui.DrawText(pDC,'Printed by Python!', -1,
  46. (0,0, int(printerwidth*.9), int(printerheight*.9)),
  47. win32con.DT_RIGHT|win32con.DT_BOTTOM|win32con.DT_SINGLELINE)
  48. win32print.EndPage(pDC)
  49. win32print.EndDoc(pDC)
  50. win32print.ClosePrinter(p)
  51. win32gui.DeleteObject(dcBM)
  52. win32gui.DeleteObject(pcBM)
  53. win32gui.DeleteObject(hf)
  54. win32gui.DeleteDC(dDC)
  55. win32gui.DeleteDC(dcDC)
  56. win32gui.DeleteDC(pDC)
  57. win32gui.DeleteDC(pcDC)