Python printing a pdf file on my Brother Laser printer (Duplex print on/off)

做~自己de王妃 提交于 2021-02-08 10:14:51

问题


I want to write a python script that prints each file in a Folder on my laser Printer. There should be the possibility to Switch the Duplex print mode on and off. The decission is made by the file Name. If there is a D in front of the file Name it is a Duplex and if there is a S it is a simplex. This I havent implemented so far.

My Problem is how do I tell the Printer to use Duplex mode?

Here is my code

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  

an alternative would be this (i have to add the Loop for all files)

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])

now I know this exists

#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  

This value can get TRUE and FALSE, but is there a way to send it when I want to print with my script?

Tahnks in advance for your help.


回答1:


You can configure the duplex setting by changing the corresponding attribute of the DevMode object. This object has other common attributes like color settings (black & white/color/..) and page orientation (lanscape/portrait) as well. Note that this works best in a get/set operation:

>>> 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

While the official Windows dev center documentation mentions that you could use PRINTER_ACCESS_MANAGE_LIMITED on line 3, win32print does not have this more restricted (but essentially all that is required) global variable defined. So you'll need full access then.

Note that you can also print using win32print, thereby saving you the trouble of using subprocess.call or "shelling out" with the win32api.

Obviously, this only works under MS Windows.

Once you have configured your printer, e.g. in the duplex mode, you can send it the list of all documents that are marked for the duplex printjob. Then you could change the setting, completely analogously to the code above, and do the same for the simplex queue.



来源:https://stackoverflow.com/questions/29118645/python-printing-a-pdf-file-on-my-brother-laser-printer-duplex-print-on-off

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