windows 使用Python打印自定义大小的纸张

weylhg0b  于 2022-12-24  发布在  Windows
关注(0)|答案(1)|浏览(522)

我已经创建了一个Windows应用程序,并连接了打印功能的按钮,我可以设置纸张大小,但问题是我不能写任何东西。

scale_factor = 20 
# 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']
devmode.PaperSize=win32con.DMPAPER_B5

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

win32print.StartDoc(hdc,('Hello',None, None, 0))
win32print.StartPage(hdc)
pen = win32gui.CreatePen(0, int(scale_factor),0)
win32gui.SelectObject(hdc,pen)

win32gui.ExtTextOut(hdc,scale_factor * 72,-1 * scale_factor * 72,1,None,"Testing..")  

win32print.EndPage(hdc)
win32print.EndDoc(hdc)

相关问题