首页 > 解决方案 > 如何使用python按顺序打印文档?

问题描述

我正在使用包来打印文档。根据打印类型,我有两个不同的文件夹。用于单面打印的文件夹 1 和用于双面打印的文件夹 2 。这是通过 win32api 包获得的,但问题是顺序打印win32api python

attributes['pDevMode'].Duplex用于设置打印模式。1 用于单面2 用于双面。最终是文件夹名称。

win32api.ShellExecute(0,'print',file_path,'.','/route',0)打印文档但不按顺序打印。

所以我从这里开始关注其他解决方案。这给了我这个错误

shell.ShellExecuteEx(fmask = win32com.shell.shellcon.SEE_MASK_NOASYNC, lpVerb='print', lpFile=file_path, lpParameters=name)
NameError: name 'win32com' is not defined

代码如下

import win32api
import win32print
import os
import win32com.shell.shell as shell
# https://stackoverflow.com/questions/47435973/print-pdf-file-in-duplex-mode-via-python
#https://stackoverflow.com/questions/18025882/how-to-determine-if-win32api-shellexecute-was-successful-using-hinstance                              
Base_Path = os.path.dirname(os.path.realpath(__file__))

name = win32print.GetDefaultPrinter()
print("------Base_Path-->",Base_Path,"-----")
print("-----GetDefaultPrinter--->",name,"-----")

for folder in "12":

    src = os.path.join(Base_Path, "admin", folder)
    for file in sorted(os.listdir(src)):
        file_path = os.path.join(src, file)
        print("------",file_path,"-----")
        printdefaults = {"DesiredAccess": win32print.PRINTER_ALL_ACCESS}
        handle = win32print.OpenPrinter(name, printdefaults)
        level = 2
        attributes = win32print.GetPrinter(handle, level)
        attributes['pDevMode'].Duplex
        attributes['pDevMode'].Duplex = int(folder)
        win32print.SetPrinter(handle, level, attributes, 0)
        win32print.GetPrinter(handle, level)['pDevMode'].Duplex
        #win32api.ShellExecute(0,'print',file_path,'.','/route',0)
        shell.ShellExecuteEx(fmask = win32com.shell.shellcon.SEE_MASK_NOASYNC, lpVerb='print', lpFile=file_path, lpParameters=name)


    win32print.ClosePrinter(handle)
    print("-----DONE FOLDEFR-------------")

print("--------DONE PRINTING-------")

标签: pythonwinapiwin32comprintersshellexecuteex

解决方案


推荐阅读