本文介绍了Python 在我的 Brother 激光打印机上打印 pdf 文件(双面打印开/关)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想编写一个 python 脚本,在我的激光打印机上打印文件夹中的每个文件.应该可以打开和关闭双面打印模式.决定是由文件名做出的.如果文件名前面有 D,则为双工,如果有 S,则为单工.到目前为止我还没有实现.

我的问题是如何告诉打印机使用双面模式?

这是我的代码

from os 导入路径从操作系统导入列表目录从 os.path 导入 isfile,加入导入 win32api导入 win32printmypath = r"D:\\test"#列出文件夹中的所有文件files = [ f for f in listdir(mypath) if isfile(join(mypath,f)) ]打印文件对于文件中的文件:文件 = mypath + "\\" + 文件## 如果文件中有11x17",文件中有县":win32api.ShellExecute (0,打印",文件,## 如果这是 None,默认打印机将# 无论如何都要使用.#'/d:"%s"' % win32print.GetDefaultPrinter(),.",0)删除文件删除我的路径

另一种方法是(我必须为所有文件添加循环)

from subprocess import callacrobat = "C:\Program Files (x86)\Adobe\Reader 11.0\Reader\AcroRd32.exe" ## Acrobat reader 显然也可以使用file = "D:\\Test\\test.pdf"打印机 = "gDoc 创建者"call([acrobat, "/T", 文件, 打印机])

现在我知道这个存在

#列出计算机上安装的所有打印机的属性和功能.导入 win32com.clientstrComputer = "."objWMIService = win32com.client.Dispatch("WbemScripting.SWbemLocator")objSWbemServices = objWMIService.ConnectServer(strComputer,"root\cimv2")colItems = objSWbemServices.ExecQuery("Select * from Win32_PrinterConfiguration")对于 colItems 中的 objItem:打印双工:",objItem.Duplex

这个值可以是 TRUE 和 FALSE,但是当我想用我的脚本打印时有没有办法发送它?

提前感谢您的帮助.

解决方案

您可以通过更改 DevMode 对象.该对象还有其他常见属性,如颜色设置(黑色和白色/彩色/..)和页面方向(横向/纵向).请注意,这在 get/set 操作中效果最好:

>>>导入 win32print>>>name = win32print.GetDefaultPrinter() # 验证它是否与您的打印机名称匹配>>>printdefaults = {"DesiredAccess": win32print.PRINTER_ALL_ACCESS} # 不适用于 PRINTER_ACCESS_USE>>>handle = win32print.OpenPrinter(name, printdefaults)>>>等级 = 2>>>属性 = win32print.GetPrinter(handle, level)>>>属性['pDevMode'].Duplex0>>>属性['pDevMode'].Duplex = 1>>>win32print.SetPrinter(句柄,级别,属性,0)>>>win32print.GetPrinter(handle, level)['pDevMode'].Duplex1

虽然 官方 Windows开发中心文档 提到您可以在第 3 行使用 PRINTER_ACCESS_MANAGE_LIMITEDwin32print 没有定义这个更受限制的(但基本上是所有必需的)全局变量.所以你需要完全访问权限.

请注意,您还可以使用win32print打印,从而避免您使用 subprocess.call 或使用 win32api 进行脱壳"的麻烦.

显然,这只适用于 MS Windows.

一旦您配置了打印机,例如在双面模式下,您可以将所有标记为双面打印作业的文档列表发送给它.然后你可以改变设置,完全类似于上面的代码,对单工队列做同样的事情.

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.

解决方案

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.

这篇关于Python 在我的 Brother 激光打印机上打印 pdf 文件(双面打印开/关)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 07:11