How to change printer preference settings using python

牧云@^-^@ 提交于 2021-02-10 20:34:39

问题


I'm new to python and want to change the printer preference settings. I'm using win32print library and need to change PyDEVMODE Object properties given on link http://timgolden.me.uk/pywin32-docs/PyDEVMODE.html as per requirement.But facing problem in crating object.

I have tried this- Python win32print changing advanced printer options

getting error "name 'pDevModeObj' is not defined"

PRINTER_DEFAULTS = {"DesiredAccess":win32print.PRINTER_ALL_ACCESS}  
pHandle = win32print.OpenPrinter('300LN1', PRINTER_DEFAULTS)  
properties = win32print.GetPrinter(pHandle, 2)  
pDevModeObj.Orientation = 2  
properties["pDevMode"]=pDevModeObj   
win32print.SetPrinter(pHandle,2,properties,0)

回答1:


This code will obviously give error.

As you are using pDevModeObj before its creation. Even in next line, you are not assigning value to pDevModeObj but rather assigning variable pDevModeObj to properties["pDevMode"], which does not work in Python.

The fix to your existing solution would be:

PRINTER_DEFAULTS = {"DesiredAccess":win32print.PRINTER_ALL_ACCESS}  
pHandle = win32print.OpenPrinter('300LN1', PRINTER_DEFAULTS)  
properties = win32print.GetPrinter(pHandle, 2)
pDevModeObj = properties["pDevMode"]
pDevModeObj.Orientation = 2  
win32print.SetPrinter(pHandle,2,properties,0)
win32print.ClosePrinter(pHandle)


来源:https://stackoverflow.com/questions/58549573/how-to-change-printer-preference-settings-using-python

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!