win32print设置打印机属性
设置打印机的属性来进行pdf打印,比如纸张大小,纸张类型,横\纵向,输入纸盘,着实费了一些时间去搜索资料。主要参考以下
-
http://timgolden.me.uk/pywin32-docs/PyDEVMODE.html
-
https://stackoverflow.com/questions/11045704/change-printer-tray-with-pywin32?r=SearchResults
-
https://blog.csdn.net/zb774095236/article/details/101058311 DEVMODE 结构体
-
https://stackoverflow.com/questions/11045704/change-printer-tray-with-pywin32?r=SearchResults
-
http://timgolden.me.uk/pywin32-docs/win32gui__CreateDC_meth.html
-
http://timgolden.me.uk/pywin32-docs/win32print__DocumentProperties_meth.html
-
http://timgolden.me.uk/pywin32-docs/DOCINFO.html
-
http://timgolden.me.uk/pywin32-docs/win32print.html
-
http://timgolden.me.uk/pywin32-docs/win32print__StartDocPrinter_meth.html
-
https://zhidao.baidu.com/question/326850334795105845.html
-
https://stackoverflow.com/questions/47435973/print-pdf-file-in-duplex-mode-via-python 设置打印机属性,即使报错也是生效的
-
https://stackoverflow.com/questions/34920866/using-devicecapabilities-with-pywin32 打印打印机的所有属性
等。
11中可以看到即使win32print.SetPrinter是失败的,也是可能设置成功的.
12中可以打印某个打印机的所有属性,
几个重要的函数接口
获取打印机属性部分:win32print.DeviceCapabilities
设置打印机部分 handle = win32print.OpenPrinter(device_name, printdefaults),其中printdefualts,表示权限,可以如下设置
printdefaults = {“DesiredAccess”: win32print.PRINTER_ALL_ACCESS}
printaccess = {“DesiredAccess”: win32print.PRINTER_ACCESS_USE}
参数PaperSize在自定义尺寸时,设置为0或者256,如果指定PaperSize大小,则PaperLength与PaoerWidth不生效。参数PaperSize的可设置值,可在win32con中查看,如
具体0还是256使得对应的打印机自定义尺寸生效,需要看对应的打印机,这个部分我是尝试得到的。
在设置例子中,MediaType的可设置值,由打印机输出的参数中选择,如DC_MEDIATYPENAMES ('普通纸和再生纸', '普通纸', '再生纸', '特殊纸', '特殊纸2', '特殊纸3', '中等厚度纸', '厚纸', '厚纸2', '厚纸3', '厚纸4', '薄纸', '投影片', '描图纸', '颜色纸', '颜色纸1', '颜色纸2', '印有信头的信纸', '印有图案的纸张', '打孔的纸张', '标签', '胶膜纸', '卡片纸', '索引纸叠', '黄色', '绿色', '蓝色', '紫色', '乳白色', '橙色', '粉红色', '红色', '灰色', '光滑纸', '信封', '明信片', '喷墨明信片', '喷墨普通纸', '铜版纸')DC_MEDIATYPES (512, 1, 514, 515, 516, 517, 522, 521, 523, 524, 525, 526, 2, 527, 528, 529, 530, 531, 532, 538, 539, 540, 541, 542, 543, 544, 545, 546, 547, 548, 549, 550, 551, 3, 559, 560, 561, 558, 562)
纸张类型和数字一一对应。设置方法见最上图。
DefaultSource 4 表示手送台, 7表示自动选择纸盘
调用 win32print.SetPrinter(handle, 2, properties, 0)来设置对应的打印机。这里就算出现异常,也可能设置成功的。
win32print.SetDefaultPrinter(device_name), 将打印机设置为默认打印机
win32api.ShellExecute(0, “print”, file_name, None, “.”, 0),最后调用函数打印文件file_name。
更新一个例子,将打印机设置为默认后打印所有文件。具体打印机的属性设置可能有微小不同:
def pdf_print_device(file_names, device_name):printaccess = {"DesiredAccess": win32print.PRINTER_ACCESS_USE}try:handle = win32print.OpenPrinter(device_name, printaccess)win32print.ClosePrinter(handle)time.sleep(2)handle = win32print.OpenPrinter(device_name, printaccess)properties = win32print.GetPrinter(handle, 2)devmode = properties['pDevMode']devmode.PaperSize = 0 # devmode.PaperLength = 3150 # .devmode.PaperWidth = 2500devmode.Duplex = win32con.DMDUP_SIMPLEX # 单面devmode.Orientation = win32con.DMORIENT_LANDSCAPE # 横向打印properties['pDevMode'] = devmodefor temp_loop in range(2):try:win32print.SetPrinter(handle, 2, properties, 0)except Exception as ff:log.info(f'setprinters:{ff}')win32print.SetDefaultPrinter(device_name)for file_name in file_names:log.info(f'{device_name}打印文件{file_name}')win32api.ShellExecute(0, "print", file_name, None, ".", 0)time.sleep(2)win32print.ClosePrinter(handle)except Exception as first:log.info(f'first:{first}')