如何使用python打印文档?

问题描述

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

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

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

所以我跟随了其他SOLUTION FROM HERE。 这给了我这个错误

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("------","-----")
        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,0)
        shell.ShellExecuteEx(fmask = win32com.shell.shellcon.SEE_MASK_NOASYNC,lpParameters=name)


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

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

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...