[英]Python printing a pdf file on my Brother Laser printer (Duplex print on/off)
我想編寫一個python腳本,將我的激光打印機上的文件夾中的每個文件打印出來。 應該有可能打開和關閉雙面打印模式。 該決定由文件名決定。 如果文件名前面有一個D,則它是一個雙工;如果有S,則是一個單工。 到目前為止,我還沒有實現。
我的問題是如何告訴打印機使用雙面模式?
這是我的代碼
from os import path
from os import listdir
from os.path import isfile, join
import win32api
import win32print
mypath = r"D:\\test"
#list all the files in a folder
files = [ f for f in listdir(mypath) if isfile(join(mypath,f)) ]
print files
for file in files:
file = mypath + "\\" + file
## if "11x17" in file and "County" in file:
win32api.ShellExecute (
0,
"print",
file,
#
# If this is None, the default printer will
# be used anyway.
#
'/d:"%s"' % win32print.GetDefaultPrinter (),
".",
0
)
del files
del mypath
一種替代方法是(我必須為所有文件添加循環)
from subprocess import call
acrobat = "C:\Program Files (x86)\Adobe\Reader 11.0\Reader\AcroRd32.exe" ## Acrobat reader would also work, apparently
file = "D:\\Test\\test.pdf"
printer = "gDoc Creator"
call([acrobat, "/T", file, printer])
現在我知道這個存在
#Lists properties and capabilities for all the printers installed on a computer.
import win32com.client
strComputer = "."
objWMIService = win32com.client.Dispatch("WbemScripting.SWbemLocator")
objSWbemServices = objWMIService.ConnectServer(strComputer,"root\cimv2")
colItems = objSWbemServices.ExecQuery("Select * from Win32_PrinterConfiguration")
for objItem in colItems:
print "Duplex: ", objItem.Duplex
該值可以為TRUE和FALSE,但是當我想使用腳本進行打印時,是否可以發送該值?
提前為您提供幫助。
您可以通過更改DevMode
對象的相應屬性來配置雙工設置。 該對象還具有其他常用屬性,例如顏色設置(黑白/顏色/ ..)和頁面方向(橫向/縱向)。 請注意,這在獲取/設置操作中效果最好:
>>> import win32print
>>> name = win32print.GetDefaultPrinter() # verify that it matches with the name of your printer
>>> printdefaults = {"DesiredAccess": win32print.PRINTER_ALL_ACCESS} # Doesn't work with PRINTER_ACCESS_USE
>>> handle = win32print.OpenPrinter(name, printdefaults)
>>> level = 2
>>> attributes = win32print.GetPrinter(handle, level)
>>> attributes['pDevMode'].Duplex
0
>>> attributes['pDevMode'].Duplex = 1
>>> win32print.SetPrinter(handle, level, attributes, 0)
>>> win32print.GetPrinter(handle, level)['pDevMode'].Duplex
1
Windows官方開發人員中心文檔提到您可以在第3行上使用PRINTER_ACCESS_MANAGE_LIMITED
,但win32print
並沒有定義更嚴格(但實際上是所有必需的)全局變量。 因此,您需要完全訪問權限。
請注意,您還可以打印使用win32print
,從而節省您使用的麻煩subprocess.call
或“脫殼而出”與win32api
。
顯然,這僅在MS Windows下有效。
一旦配置好打印機(例如在雙面打印模式下),就可以向其發送標記為雙面打印作業的所有文檔的列表。 然后,您可以完全類似於上面的代碼來更改設置,並對單工隊列執行相同的操作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.