使用 python 设置默认打印机自定义页面大小
setting default printer custom page size with python
在我工作的组织中,不同的打印机设置在不同的位置。都是主要用于打印A4尺寸的文档,所以默认设置相应。
我们还使用了一堆自定义大小的表格,人们迄今为止一直在手工填写这些表格。
最近,我的任务是在我们的中央数据库中为上述表格设置打印自动化。
我正在使用 reportlab 创建临时 pdf 文件,然后尝试将其发送到默认打印机。一切都相对简单,除了让打印机注册自定义纸张尺寸。
我得到了以下代码片段,但我真的卡住了。
import tempfile
import win32api
import win32print
pdf_file = tempfile.mktemp(".pdf")
#CREATION OF PDF FILE WITH REPORTLAB
printer = win32print.GetDefaultPrinter()
PRINTER_DEFAULTS = {"DesiredAccess":win32print.PRINTER_ALL_ACCESS}
pHandle = win32print.OpenPrinter(printer, PRINTER_DEFAULTS)
level = 2
properties = win32print.GetPrinter(pHandle, level)
pDevModeObj = properties["pDevMode"]
pDevModeObj.PaperSize = 0
pDevModeObj.PaperLength = 2200 #SIZE IN 1/10 mm
pDevModeObj.PaperWidth = 1000 #SIZE IN 1/10 mm
properties["pDevMode"]=pDevModeObj
win32print.SetPrinter(pHandle,level,properties,0)
#OPTION ONE
#win32api.ShellExecute(0, "print", pdf_file, None, ".", 0)
#OPTION TWO
win32api.ShellExecute (0,"printto",pdf_file,'"%s"' % printer,".",0)
win32print.ClosePrinter(pHandle)
只是行不通。打印机不会报告“纸张尺寸不匹配”,就像在向他们发送非 A4 文档时那样。当我尝试打印到 PDF 打印机时,它也默认为 A4。
调用时
print(pDevModeObj.PaperSize)
print(pDevModeObj.PaperLength)
print(pDevModeObj.PaperWidth)
一切似乎都井井有条,所以我猜我不知道如何将这些纸张尺寸值发送回打印机设置。
这是我检查过的所有资源的列表(示例不在 python 中,还有一些没有使用 win32api),但无法正常工作:
- Programmatically Print a PDF File - Specifying Printer
- Python's win32api only printing to default printer
- https://mail.python.org/pipermail/python-win32/2005-August/003683.html
- https://docs.microsoft.com/en-us/troubleshoot/windows/win32/modify-printer-settings-setprinter-api
- https://www.thinbug.com/q/39249360
- Saving / Restoring Printer DevModes - wxPython / win32print
- pywin32: how do I get a pyDEVMODE object?
- https://docs.microsoft.com/en-us/troubleshoot/windows/win32/modify-printer-settings-documentproperties
- How to change printer preference settings using python
- Print file to continuous paper using win32print Python
- python win32print can't set custom page size
- http://timgolden.me.uk/pywin32-docs/PyDEVMODE.html
- https://newcenturycomputers.net/projects/pythonicwindowsprinting.html
- Printing a file and configure printer settings
- Change printer default paper size
- https://grokbase.com/t/python/python-win32/085x5hdbtd/how-to-change-paper-size-while-printing
- openpyxl - set custom paper size for printing
- Python win32print changing advanced printer options
- Python silent print PDF to specific printer
- https://docs.microsoft.com/en-us/windows/win32/cimwin32prov/win32-printerconfiguration
- Printing PDF's using Python,win32api, and Acrobat Reader 9
- Python print pdf file with win32print
- https://www.programcreek.com/python/example/24860/win32api.ShellExecute
- https://opensource.gonnerman.org/?p=192
- Python27 - on windows 10 how can i tell printing paper size is 50.8mm x 25.4mm?
- https://mail.python.org/pipermail/python-win32/2008-May/007640.html
- http://timgolden.me.uk/python/win32_how_do_i/print.html
ShellExecute
使用默认打印参数。如果需要使用reset DevMode
进行打印,可以使用CreateDC
.
If you use SetPrinter to modify the default DEVMODE structure for a
printer (globally setting the printer defaults), you must first call
the DocumentProperties function to validate the DEVMODE structure.
参考:
- SetPrinter Remarks
- Modify printer settings by using the SetPrinter function
也可以直接使用DocumentProperties
修改打印机初始化信息
然后将pDevModeObj
传递给CreateDC
,并使用StartDoc
和StartPage
进行打印。
相似案例:Change printer tray with pywin32
在我工作的组织中,不同的打印机设置在不同的位置。都是主要用于打印A4尺寸的文档,所以默认设置相应。
我们还使用了一堆自定义大小的表格,人们迄今为止一直在手工填写这些表格。 最近,我的任务是在我们的中央数据库中为上述表格设置打印自动化。
我正在使用 reportlab 创建临时 pdf 文件,然后尝试将其发送到默认打印机。一切都相对简单,除了让打印机注册自定义纸张尺寸。
我得到了以下代码片段,但我真的卡住了。
import tempfile
import win32api
import win32print
pdf_file = tempfile.mktemp(".pdf")
#CREATION OF PDF FILE WITH REPORTLAB
printer = win32print.GetDefaultPrinter()
PRINTER_DEFAULTS = {"DesiredAccess":win32print.PRINTER_ALL_ACCESS}
pHandle = win32print.OpenPrinter(printer, PRINTER_DEFAULTS)
level = 2
properties = win32print.GetPrinter(pHandle, level)
pDevModeObj = properties["pDevMode"]
pDevModeObj.PaperSize = 0
pDevModeObj.PaperLength = 2200 #SIZE IN 1/10 mm
pDevModeObj.PaperWidth = 1000 #SIZE IN 1/10 mm
properties["pDevMode"]=pDevModeObj
win32print.SetPrinter(pHandle,level,properties,0)
#OPTION ONE
#win32api.ShellExecute(0, "print", pdf_file, None, ".", 0)
#OPTION TWO
win32api.ShellExecute (0,"printto",pdf_file,'"%s"' % printer,".",0)
win32print.ClosePrinter(pHandle)
只是行不通。打印机不会报告“纸张尺寸不匹配”,就像在向他们发送非 A4 文档时那样。当我尝试打印到 PDF 打印机时,它也默认为 A4。
调用时
print(pDevModeObj.PaperSize)
print(pDevModeObj.PaperLength)
print(pDevModeObj.PaperWidth)
一切似乎都井井有条,所以我猜我不知道如何将这些纸张尺寸值发送回打印机设置。
这是我检查过的所有资源的列表(示例不在 python 中,还有一些没有使用 win32api),但无法正常工作:
- Programmatically Print a PDF File - Specifying Printer
- Python's win32api only printing to default printer
- https://mail.python.org/pipermail/python-win32/2005-August/003683.html
- https://docs.microsoft.com/en-us/troubleshoot/windows/win32/modify-printer-settings-setprinter-api
- https://www.thinbug.com/q/39249360
- Saving / Restoring Printer DevModes - wxPython / win32print
- pywin32: how do I get a pyDEVMODE object?
- https://docs.microsoft.com/en-us/troubleshoot/windows/win32/modify-printer-settings-documentproperties
- How to change printer preference settings using python
- Print file to continuous paper using win32print Python
- python win32print can't set custom page size
- http://timgolden.me.uk/pywin32-docs/PyDEVMODE.html
- https://newcenturycomputers.net/projects/pythonicwindowsprinting.html
- Printing a file and configure printer settings
- Change printer default paper size
- https://grokbase.com/t/python/python-win32/085x5hdbtd/how-to-change-paper-size-while-printing
- openpyxl - set custom paper size for printing
- Python win32print changing advanced printer options
- Python silent print PDF to specific printer
- https://docs.microsoft.com/en-us/windows/win32/cimwin32prov/win32-printerconfiguration
- Printing PDF's using Python,win32api, and Acrobat Reader 9
- Python print pdf file with win32print
- https://www.programcreek.com/python/example/24860/win32api.ShellExecute
- https://opensource.gonnerman.org/?p=192
- Python27 - on windows 10 how can i tell printing paper size is 50.8mm x 25.4mm?
- https://mail.python.org/pipermail/python-win32/2008-May/007640.html
- http://timgolden.me.uk/python/win32_how_do_i/print.html
ShellExecute
使用默认打印参数。如果需要使用reset DevMode
进行打印,可以使用CreateDC
.
If you use SetPrinter to modify the default DEVMODE structure for a printer (globally setting the printer defaults), you must first call the DocumentProperties function to validate the DEVMODE structure.
参考:
- SetPrinter Remarks
- Modify printer settings by using the SetPrinter function
也可以直接使用DocumentProperties
修改打印机初始化信息
然后将pDevModeObj
传递给CreateDC
,并使用StartDoc
和StartPage
进行打印。
相似案例:Change printer tray with pywin32