首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Python静默将PDF打印到特定打印机

Python静默将PDF打印到特定打印机
EN

Stack Overflow用户
提问于 2014-11-29 04:38:13
回答 8查看 37.8K关注 0票数 18

我有一个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打开

代码语言:javascript
复制
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'命令时出现异常。

EN

回答 8

Stack Overflow用户

回答已采纳

发布于 2014-11-29 06:24:46

经过几个小时的搜索,我终于找到了问题的答案。

您可以在HERE中下载GSPRINT

您可以在HERE中下载Ghostscript GPL

在您的PC (Windows)中提取这些文件后,您可以使用以下命令打印您的PDF

代码语言:javascript
复制
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

我希望这能帮到你。

票数 22
EN

Stack Overflow用户

发布于 2018-07-13 20:40:20

这里有一种方法,可以在不使用gsprintwin32api的情况下,在与python脚本相同的目录中静默打印pdf。它允许更多的GhostScript定制,比如选择宽度/高度等。

代码语言:javascript
复制
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

票数 2
EN

Stack Overflow用户

发布于 2019-10-10 14:52:36

下面的代码将阻塞当前任务

代码语言:javascript
复制
for i in range(10):
    currentprinter = win32print.GetDefaultPrinter()
    win32api.ShellExecute(0, "print", 'PDFfile.pdf', '/d:"%s"' % currentprinter, ".", 0)

并且在打印帮助后杀死阅读器不会阻止当前任务

代码语言:javascript
复制
os.system("TASKKILL /F /IM AcroRD32.exe") 

但它也会关闭其他pdf文件。

如果不能使用gsprint,可以使用acrobat命令

代码语言:javascript
复制
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文件。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27195594

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档