我有一个PDF文档,我想用我的python应用程序打印它。
我已经在here (Print PDF document with python's win32print module?)中尝试过该解决方案,但是当我安装Ghostscript9.15时,它没有gsprint.exe
我使用的方式是使用命令os.startfile('PDFfile.pdf', "print")
,但它打开默认的查看器(我的是Adobe Reader),打印后它仍然是打开的,试图用os.system("TASKKILL /F /IM AcroRD32.exe")
杀死进程会杀死其他打开的窗口,我不想要它。
使用下一个命令,它也会打印出来,但它也会让Adobe Reader打开
currentprinter = win32print.GetDefaultPrinter()
win32api.ShellExecute(0, "print", 'PDFfile.pdf', '/d:"%s"' % currentprinter, ".", 0)
我也看过this answer,但他们建议再次使用gsprint.exe
谁有gsprint.exe
文件或其他解决方案?
注意:当我使用另一个默认程序打开'(31, 'ShellExecute', 'A device attached to the system is not functioning.')'
或Windows Reader等PDF文件时,我总是在使用startfile
命令执行上述'(31, 'ShellExecute', 'A device attached to the system is not functioning.')'
或[Error 1155] No application is associated with the specified file for this operation: 'PDFfile.pdf'
命令时出现异常。
发布于 2014-11-29 06:24:46
经过几个小时的搜索,我终于找到了问题的答案。
您可以在HERE中下载GSPRINT
您可以在HERE中下载Ghostscript GPL
在您的PC (Windows)中提取这些文件后,您可以使用以下命令打印您的PDF
GHOSTSCRIPT_PATH = "C:\\path\\to\\GHOSTSCRIPT\\bin\\gswin32.exe"
GSPRINT_PATH = "C:\\path\\to\\GSPRINT\\gsprint.exe"
# YOU CAN PUT HERE THE NAME OF YOUR SPECIFIC PRINTER INSTEAD OF DEFAULT
currentprinter = win32print.GetDefaultPrinter()
win32api.ShellExecute(0, 'open', GSPRINT_PATH, '-ghostscript "'+GHOSTSCRIPT_PATH+'" -printer "'+currentprinter+'" "PDFFile.pdf"', '.', 0)
还可以在官方页面HERE中找到GhostScript
我找到了64位HERE的gsprint.exe
我希望这能帮到你。
发布于 2018-07-13 20:40:20
这里有一种方法,可以在不使用gsprint
和win32api
的情况下,在与python脚本相同的目录中静默打印pdf。它允许更多的GhostScript定制,比如选择宽度/高度等。
import os
import subprocess
import sys
if sys.platform == 'win32':
args = '"C:\\\\Program Files\\\\gs\\\\gs9.23\\\\bin\\\\gswin64c" ' \
'-sDEVICE=mswinpr2 ' \
'-dBATCH ' \
'-dNOPAUSE ' \
'-dFitPage ' \
'-sOutputFile="%printer%myPrinterName" '
ghostscript = args + os.path.join(os.getcwd(), 'myFile.pdf').replace('\\', '\\\\')
subprocess.call(ghostscript, shell=True)
如果您使用的是32位版本的GhostScript,那么您应该使用gswin32c
发布于 2019-10-10 14:52:36
下面的代码将阻塞当前任务
for i in range(10):
currentprinter = win32print.GetDefaultPrinter()
win32api.ShellExecute(0, "print", 'PDFfile.pdf', '/d:"%s"' % currentprinter, ".", 0)
并且在打印帮助后杀死阅读器不会阻止当前任务
os.system("TASKKILL /F /IM AcroRD32.exe")
但它也会关闭其他pdf文件。
如果不能使用gsprint,可以使用acrobat命令
import win32print
import subprocess
import time
pdf_file = 'D:\d1\d1.pdf'
acrobat = 'C:\Program Files (x86)\Adobe\Acrobat Reader DC\Reader\AcroRd32.exe'
name = win32print.GetDefaultPrinter()
cmd = '"{}" /n /o /t "{}" "{}"'.format(acrobat, pdf_file, name)
for i in range(10)):
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
它不会阻止当前任务并关闭其他pdf文件。
https://stackoverflow.com/questions/27195594
复制相似问题