-
2021-10-08 19:11:47
#需要先安装win32库
import win32api
import win32printwin32api.ShellExecute(0,“print”,filename,’/d:"%s"’ % win32print.GetDefaultPrinter(),".",0)
#filename一定要使用绝对路径,否则会报找不到文件的错误更多相关内容 -
python打印pdf文件_使用win32print的Python打印pdf文件
2020-11-24 11:10:49I'm trying to print a pdf file from Python with module win32print but the only way I can print success is a text.hPrinter = win32print.OpenPrinter("\\\\Server\Printer")filename = "test.pdf"try:hJob = ...I'm trying to print a pdf file from Python with module win32print but the only way I can print success is a text.
hPrinter = win32print.OpenPrinter("\\\\Server\Printer")
filename = "test.pdf"
try:
hJob = win32print.StartDocPrinter(hPrinter, 1, ('PrintJobName', None, 'RAW'))
try:
win32api.ShellExecute(0, "print", filename, None, ".", 0)
win32print.StartPagePrinter(hPrinter)
win32print.WritePrinter(hPrinter, "test") # Instead of raw text is there a way to print PDF File ?
win32print.EndPagePrinter(hPrinter)
finally:
win32print.EndDocPrinter(hPrinter)
finally:
win32print.ClosePrinter(hPrinter)
So instead of printing a text I need to print the "test.pdf" file.
I also tried with win32api.ShellExecute(0, "print", filename, None, ".", 0) but it's not working, after some test like (getprinter, getdefault, setprinter, setdefaultprinter) it seems not to be attaching the printer. So at this way I can't get working.
This is the code I used !
win32print.SetDefaultPrinter(hPrinter)
win32api.ShellExecute(0, "print", filename, None, ".", 0)
解决方案
This is the code I have used and it works correctly.
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 = 1 #no flip
#attributes['pDevMode'].Duplex = 2 #flip up
attributes['pDevMode'].Duplex = 3 #flip over
win32print.SetPrinter(handle, level, attributes, 0)
win32print.GetPrinter(handle, level)['pDevMode'].Duplex
win32api.ShellExecute(0,'print','manual1.pdf','.','/manualstoprint',0)
-
python调用打印机打印pdf文件第三方包
2019-04-28 15:14:48python通过pywin32执行调用打印机打印pdf文件,压缩包内两个文件,一个是gs927w64.exe安装包,一个解压好直接用的GSPRINT压缩包 -
用Python静默打印PDF
2021-07-19 16:56:28I'm trying to print a PDF with Python, without opening the PDF viewer application (Adobe, Foxit etc.). I need also to know when printing has finished (to delete the file).Here I found this implementat...I'm trying to print a PDF with Python, without opening the PDF viewer application (Adobe, Foxit etc.). I need also to know when printing has finished (to delete the file).
Here I found this implementation:
import win32ui, dde, os.path, time
from win32api import FindExecutable
from os import spawnl, P_NOWAIT
...
pd = "C:\\temp\\test.pdf"
pdbits = os.path.split(pd)
readerexe = FindExecutable(pdbits[1],pdbits[0])
spawnl(P_NOWAIT,readerexe[1],"DUMMY") #I added "DUMMY" to avoid a weird error
time.sleep(2)
s = dde.CreateServer()
s.Create('')
c = dde.CreateConversation(s)
c.ConnectTo('acroview', 'control')
c.Exec('[FilePrintSilent("%s")]' % (pd,))
s.Destroy()
But it throws this exception at the ConnectTo line:
dde.error: ConnectTo failed
Someone knows how to solve it? Or has a different solution for silent printing? Or at list can give a link to a reference for ConnectTo? Could find nothing on the web about it.
Working with: Python 2.7, Windows 7, Acrobat Reader 10.0
解决方案
I suggest you install GSView and GSPrint and shell out to gsprint.exe to print the pdf.
p = subprocess.Popen([r"p:\ath\to\gsprint.exe", "test.pdf"],
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = p.communicate()
print stdout
print stderr
I have used this in a industrial label printing solution, works great.
When the gsprint.exe program exits (i.e. after the call to communicate), you can delete the pdf file.
-
在Python中静默打印PDF
2021-04-27 11:32:00我正在尝试使用Python打印PDF,而无需打开PDF查看器应用程序(Adobe,Foxit等)。 我还需要知道何时打印完成(删除文件)。在这里我find了这个实现 :import win32ui, dde, os.path, time from win32api import Find...我正在尝试使用Python打印PDF,而无需打开PDF查看器应用程序(Adobe,Foxit等)。 我还需要知道何时打印完成(删除文件)。
在这里我find了这个实现 :
import win32ui, dde, os.path, time from win32api import FindExecutable from os import spawnl, P_NOWAIT ... pd = "C:\\temp\\test.pdf" pdbits = os.path.split(pd) readerexe = FindExecutable(pdbits[1],pdbits[0]) spawnl(P_NOWAIT,readerexe[1],"DUMMY") #I added "DUMMY" to avoid a weird error time.sleep(2) s = dde.CreateServer() s.Create('') c = dde.CreateConversation(s) c.ConnectTo('acroview', 'control') c.Exec('[FilePrintSilent("%s")]' % (pd,)) s.Destroy()
但是它在ConnectTo行引发这个exception:
dde.error: ConnectTo failed
有人知道如何解决它? 或者有一个不同的静音打印解决scheme ? 或者在列表中可以给出ConnectTo的参考链接? 在网上找不到关于它的东西。
使用:Python 2.7,Windows 7,Acrobat Reader 10.0
我建议你安装GSView和GSPrint和shell到gsprint.exe来打印pdf。
p = subprocess.Popen([r"p:\ath\to\gsprint.exe", "test.pdf"], stdout=subprocess.PIPE, stderr=subprocess.PIPE) stdout, stderr = p.communicate() print stdout print stderr
我已经在一个工业标签打印解决方案中使用这个,效果很好。
当gsprint.exe程序退出(即在communicate ),您可以删除PDF文件。
-
Python后台静默打印PDF文件附属执行程序
2021-12-03 15:39:33本资源内涵ghostscript及gsprint可执行程序,助于实现打印机后台静默打印所需要的exe文件。 -
Python静默打印PDF到特定的打印机
2020-11-24 11:10:49我有一个PDF文档,我想用我的python应用程序打印它.我使用的方式是使用命令os.startfile(‘PDFfile.pdf’,“print”),但它打开默认查看器(我的是Adobe Reader),打印后它仍然打开,试图用os杀死进程.system(“TASKKILL ... -
python 批量打印PDF(转)
2022-02-25 21:27:46安装Ghostscript,GhostView,使用gsprint命令打印pdf文件。 gsprint命令参数说明: "-dQUIET", 安静的意思,指代执行过程中尽可能少的输出日志等信息。(也可以简写为“-q”) "-dNOSAFER", 通过命令行运行 "-... -
使用win32print的Python打印pdf文件
2021-07-16 12:54:28I'm trying to print a pdf file from Python with module win32print but the only way I can print success is a text.hPrinter = win32print.OpenPrinter("\\\\Server\Printer")filename = "test.pdf"try:hJob = ... -
python静默打印pdf
2018-12-02 21:27:20import win32api import win32print def print_pdf(self, pdf_file_name): "&... 静默打印pdf :param pdf_file_name: :return: "& -
通过Python以双工模式打印PDF文件
2021-02-11 00:30:51win32print.SetPrinter(handle, level, attributes, 0) except: print "win32print.SetPrinter: set 'Duplex'" res = win32api.ShellExecute(0, 'print', 'test.pdf', None, '.', 0) win32print.ClosePrinter... -
python输出pdf文档的实例
2021-04-27 05:52:15python导出pdf,参考诸多资料,发现pdfkit是效果比较好的。故下载后进行了实现,多次失败后终于成功了,现将其中经验总结如下:"""需要安装pdfkit,另外需要安装可执行文件wkhtmltopdf.exe,pdfkit核心命令是调用... -
python – 打印PDF并在打印完成后删除文件
2020-11-24 11:10:18它将PDF保存为文件,然后将其打印出来.打印结束时删除文件.我目前的解决方案(用于打印和删除部分)是这样的:win32api.ShellExecute(0, "print", file_path, None, ".", 0)time.sleep(10)os.remove(self.options.dest_... -
3条指令教会你用python创建中文pdf文件,适合批量打印,附送源码-pdf文件不能打印
2020-11-24 11:10:45python功能强大,支持开发网站或者用于本地数据处理,经常要输出一些文档资料供给用户打印之用。网站常用网页直接打印,本地常输出到excel文件排版输出。其实python经过适当的配置,输出中文pdf文件的效果也是极好的... -
python连接打印机实现打印文档、图片、pdf文件等功能
2020-09-18 00:14:12主要介绍了python连接打印机实现打印文档、图片、pdf文件等功能,需要的朋友可以参考下 -
python打印pdf元数据
2020-09-05 15:49:46parser.add_argument('file',help='the filename',type=str) args=parser.parse_args() printMeta(args.file) def printMeta(filename): pdf=open(filename,'rb') pdfFile=PdfFileReader(pdf) docInfo=pdfFile.... -
Python真正实现PDF按顺序静默打印
2021-05-17 11:21:47由于工作需求,需要按照固定顺序打印大量的PDF文件. 第一时间想到的就是用Python的模块来解决.查阅了大量的文章之后,开始了我的测试之旅… 一.第一种方案.ghostscript与gsprint打印.代码如下: def print_pdf(x): ... -
python连接打印机打印文档、图片、pdf文件等
2019-08-27 10:25:50引言 python连接打印机进行打印,可能根据需求的不同,使用不同的函数模块。 如果你只是简单的想打印文档,比如 -
将python代码打印成pdf
2021-02-02 23:53:54利用反射调用方法时,处理ref,out参数需要注意的问题(转)转自:http://www.68idc.cn/help/buildlang/ask/20150318283817.html 项目中如下的泛型方法,因为要在运行时,动态指定类型参数,所以要利用反射来实现 ...Outer... -
Python – 打开特定页面/部分的pdf文件
2021-02-10 13:06:24这是两个基本的想法案例1:您想要用Python打开文件from pyPdf import PdfFileReader, PageObjectpdf_toread = PdfFileReader(path_to_your_pdf)# 1 is the number of the pagepage_one = pdf_toread.getPage(1)# ... -
教会你所有的Python模块使用PDF打印.pdf
2020-08-08 00:16:02一 寸 光 阴 不 可 轻 看了这篇你就会了所有的python 模 块使用 如果你退出 Python 解释器并重新进入你做的任何定义变量和方法都 会丢失因此如果你想要编写一些更大的程序为准备解释器输入使用一个 文本编辑器会更好... -
Python | 实现pdf文件分页
2022-01-01 14:23:28pdf的分页,我们在现实生活中,是难免会遇到的事。当你遇到时,你是怎么解决的呢?在Python中,实现pdf分页,是极其简单快速的,只需要运行几行代码,即可实现,不管你的pdf文件有多大,下面让我们一起来看看吧~ -
pdf静默打印依赖包
2018-12-02 21:23:50python在windows平台下实现pdf静默所依赖的安装包。其中包括GSPRINT和Ghostscript -
python 批量打印PDF
2020-12-03 14:51:52网上搜索一波,方案如下:安装Ghostscript,GhostView,使用gsprint命令打印pdf文件。gsprint命令参数说明:"-dQUIET", 安静的意思,指代执行过程中尽可能少的输出日志等信息。(也可以简写为“-q”)"-dNOSAFER", 通过... -
通过python实现批量excel转pdf代码
2018-08-23 16:59:52通过python实现批量excel转pdf代码。 -
如何利用python在Linux系统下静默打印pdf?注意点:Linux系统下。
2019-08-26 08:59:47想要通过python语言静默打印pdf,利用win32api.ShellExecute已经实现在Windows下的静默打印,还没有思路在Linux系统下该如何操作,谢谢大家了TwT.ps:非静默打印也行,一步一步的慢慢走 -
GSPRINT静默打印pdf
2018-11-22 14:27:17GSPRINT用于搭配Ghostscript一起使用,可使用python静默打印pdf -
基于Python快速处理PDF表格数据
2020-12-17 16:16:42我们有下面一张PDF格式存储的表格,现在需要使用Python将它提取出来。 使用Python提取表格数据需要使用pdfplumber模块,打开CMD,安装代码如下: pip install pdfplumber 安装完之后,将需要使用的模块导入 import ...