windows 打印 PDF 并在打印完成后删除文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4469629/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Print a PDF and delete the file when printing has finished
提问by bluish
I have a Python application taht will be executed repeatedly. It saves a PDF as a file and then prints it. When printing ends it deletes the file.
我有一个将重复执行的 Python 应用程序。它将 PDF 保存为文件,然后进行打印。打印结束时,它会删除文件。
My current solution(for the print and delete part) is this:
我目前的解决方案(用于打印和删除部分)是这样的:
win32api.ShellExecute(0, "print", file_path, None, ".", 0)
time.sleep(10)
os.remove(self.options.dest_name)
time.sleep(10)
is a trickto give the printing process the time to run before file deletion. Without it Acrobat Reader opens (it opens anyway) and alerts that it can't find the file. This because file removal has already occured.
time.sleep(10)
是在文件删除之前让打印过程有时间运行的技巧。如果没有它,Acrobat Reader 会打开(无论如何它都会打开)并警告它找不到文件。这是因为文件删除已经发生。
The questionis:
该问题是:
how can I do it without this unreliable trick? The best thing would be to have an handler for the printing process and get by it an info about the printing state: I wait for it to report it's completed and I delete the file.
it would be even better if Acrobat Reader wouldn't open, but this is not a great problem.
如果没有这个不可靠的技巧,我怎么能做到呢?最好的办法是为打印过程设置一个处理程序并通过它获取有关打印状态的信息:我等待它报告它已完成并删除文件。
如果 Acrobat Reader 打不开就更好了,但这不是什么大问题。
EDIT: I tried switching to Foxit Reader as the default PDF reader and now it doesn't open when I don't want. ;)
编辑:我尝试切换到 Foxit Reader 作为默认的 PDF 阅读器,现在它在我不想要的时候不会打开。;)
OTHER POSSIBLE SOLUTION: Cylically check if the file is available (not used by another process) and when it's available again delete it. How could I do it in Python?
其他可能的解决方案:循环检查文件是否可用(未被另一个进程使用),当它再次可用时将其删除。我怎么能用 Python 做到这一点?
回答by bluish
At last I've found a good solution, thanks to this answer(and also @Lennart mentioned it on a comment):
最后,我找到了一个很好的解决方案,这要归功于这个答案(@Lennart 在评论中也提到了它):
install GSview(which includes gsprint.exe
)
安装 GSview(包括gsprint.exe
)
write this code:
写这个代码:
file_path = "C:\temp\test.pdf"
p = subprocess.Popen(["C:\Ghostgum\gsview\gsprint.exe", "-printer", printer_name, "-colour", file_path],
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = p.communicate() # waits for the gs process to end
os.remove(file_path) # now the file can be removed
No Acrobat windows opening, no file removed before printing... The annoyance: installing GS.
没有打开 Acrobat 窗口,没有在打印前删除文件... 烦恼:安装 GS。
See also: gsprint
reference
另见:gsprint
参考
回答by Daniel Roseman
Rather than hard-coding a filename and printing that, you should use the tempfile
module to create a temporary file with a unique name.
您应该使用该tempfile
模块创建一个具有唯一名称的临时文件,而不是对文件名进行硬编码并打印出来。
import tempfile
file_name = tempfile.NamedTemporaryFile(suffix=".pdf", delete=False)
If you want, you can run a regular tidy-up script using Window's scheduling tools to delete the files created.
如果需要,您可以使用 Window 的调度工具运行定期整理脚本来删除创建的文件。
回答by Lennart Regebro
Adobe acrobat has (or at least used to have) a parameter "/t", which made it open, print and exit. By using it, you can call acrobat reader and wait for it to exit, and then delete the file.
Adobe acrobat 有(或至少曾经有)一个参数“/t”,它可以打开、打印和退出。通过使用它,您可以调用 acrobat reader 并等待它退出,然后删除该文件。
Untested code:
未经测试的代码:
>>> import subprocess
# You will have to figure out where your Acrobate reader is located, can be found in the registry:
>>> acrobatexe = "C:\Program Files\Adobe\Acrobat 4.0\Reader\AcroRd32.exe"
>>> subprocess.call([acrobatexe, "/t", tempfilename, "My Windows Printer Name"])
>>> os.unlink(tempfilename)
Something like that.
类似的东西。
If you don't want acrobat to open, there are open source software that will print pdfs from the command line. You could include one with your software.
如果您不想打开 acrobat,可以使用开源软件从命令行打印 pdf。您可以在您的软件中包含一个。
回答by JasonFruit
Why not use os.system
, which will wait until the process is finished?
为什么不使用os.system
,它将等到进程完成?