通过Python以双工模式打印PDF文件 [英] Print PDF file in duplex mode via Python
问题描述
我有一个Python脚本,可以打印PDF文件.
该脚本可以使用win32api.ShellExecute()
正常运行,但是一切都很好,但是现在,我需要打印包含双面内容的PDF文件,具体请参见用户手册.
我曾尝试在win32print中设置双面打印模式,但没有任何效果,打印机仍然在2张纸上为我的PDF打印2页,而不是在双面纸上打印2页.
打印机可以在其他应用程序中使用此模式,但是使用python脚本不能很好地工作.
这是我用来打印的代码的一部分:
I have an script in Python that prints PDF files.
The script works using win32api.ShellExecute()
and everything is fine, but now, I need to print PDF files that have double sided content, user manuals in concrete.
I have tried setting the duplex mode in win32print, but nothing works, the printer still print 2 pages on 2 sheets for my PDF instead of two pages on a double sided sheet.
The printer works with this mode in other applications, but with the python script doesn't work well.
This is part of the code I used to print:
name = win32print.GetDefaultPrinter()
printdefaults = {"DesiredAccess": win32print.PRINTER_ALL_ACCESS}
handle = win32print.OpenPrinter(name, printdefaults)
level = 2
attributes = win32print.GetPrinter(handle, level)
attributes['pDevMode'].Duplex
attributes['pDevMode'].Duplex = 1
win32print.SetPrinter(handle, level, attributes, 0)
win32print.GetPrinter(handle, level)['pDevMode'].Duplex
win32api.ShellExecute(0,'print','file.pdf','.','/route',0)
有人知道为什么这行不通吗?谢谢.
Any idea why this doesn't works? Thanks.
推荐答案
尝试运行此代码:
import win32api
import win32print
name = win32print.GetDefaultPrinter()
#printdefaults = {"DesiredAccess": win32print.PRINTER_ACCESS_ADMINISTER}
printdefaults = {"DesiredAccess": win32print.PRINTER_ACCESS_USE}
handle = win32print.OpenPrinter(name, printdefaults)
level = 2
attributes = win32print.GetPrinter(handle, level)
print "Old Duplex = %d" % attributes['pDevMode'].Duplex
#attributes['pDevMode'].Duplex = 1 # no flip
#attributes['pDevMode'].Duplex = 2 # flip up
attributes['pDevMode'].Duplex = 3 # flip over
## 'SetPrinter' fails because of 'Access is denied.'
## But the attribute 'Duplex' is set correctly
try:
win32print.SetPrinter(handle, level, attributes, 0)
except:
print "win32print.SetPrinter: set 'Duplex'"
res = win32api.ShellExecute(0, 'print', 'test.pdf', None, '.', 0)
win32print.ClosePrinter(handle)
它适用于我的计算机:Windows 10,Python 2.7.14,pypiwin32-220
It works on my computer: Windows 10, Python 2.7.14, pypiwin32-220
注意:
- 在我的计算机上PRINTER_ACCESS_ADMINISTER导致 OpenPrinter.
- 在我的计算机上,SetPrinter失败,并显示访问被拒绝".但是双工"是 设置正确..
- On my computer PRINTER_ACCESS_ADMINISTER causes 'Access is denied' in OpenPrinter.
- On my computer SetPrinter fails with 'Access is denied'. But 'Duplex' is set correctly..
这篇关于通过Python以双工模式打印PDF文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!