Advertisement
Guest User

Untitled

a guest
Feb 14th, 2016
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.76 KB | None | 0 0
  1. import os
  2. os.startfile("C:/Users/Jdash/Desktop/TestFile.txt", "print")
  3.  
  4. # create a dc (Device Context) object (actually a PyCDC)
  5. dc = win32ui.CreateDC()
  6.  
  7. # convert the dc into a "printer dc"
  8.  
  9. # get default printer
  10. printername = win32print.GetDefaultPrinter ()
  11. # leave out the printername to get the default printer automatically
  12. dc.CreatePrinterDC(printername)
  13.  
  14. # you need to set the map mode mainly so you know how
  15. # to scale your output. I do everything in points, so setting
  16. # the map mode as "twips" works for me.
  17. dc.SetMapMode(win32con.MM_TWIPS) # 1440 per inch
  18.  
  19. # here's that scaling I mentioned:
  20. scale_factor = 20 # i.e. 20 twips to the point
  21.  
  22. # start the document. the description variable is a string
  23. # which will appear in the print queue to identify the job.
  24. dc.StartDoc('Win32print test')
  25.  
  26. # to draw anything (other than text) you need a pen.
  27. # the variables are pen style, pen width and pen color.
  28. pen = win32ui.CreatePen(0, int(scale_factor), 0)
  29.  
  30. # SelectObject is used to apply a pen or font object to a dc.
  31. dc.SelectObject(pen)
  32.  
  33. # how about a font? Lucida Console 10 point.
  34. # I'm unsure how to tell if this failed.
  35. font = win32ui.CreateFont({
  36. "name": "Lucida Console",
  37. "height": int(scale_factor * 10),
  38. "weight": 400,
  39. })
  40.  
  41. # again with the SelectObject call.
  42. dc.SelectObject(font)
  43.  
  44. # okay, now let's print something.
  45. # TextOut takes x, y, and text values.
  46. # the map mode determines whether y increases in an
  47. # upward or downward direction; in MM_TWIPS mode, it
  48. # advances up, so negative numbers are required to
  49. # go down the page. If anyone knows why this is a
  50. # "good idea" please email me; as far as I'm concerned
  51. # it's garbage.
  52. dc.TextOut(scale_factor * 72,
  53. -1 * scale_factor * 72,
  54. "Testing...")
  55.  
  56. # must not forget to tell Windows we're done.
  57. dc.EndDoc()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement