[英]How do I close a pdf file once it has printed in python?
I am wondering how to close a pdf file once it has printed.我想知道打印后如何关闭 pdf 文件。 I am using subprocess.popen to print the file but I need to physically close the acrobat application before it can go to the next file (as i believe the subprocess doesn't recognise that the process has already completed and is waiting for the user to close the application).我正在使用 subprocess.popen 来打印文件,但我需要在它可以 go 到下一个文件之前物理关闭 acrobat 应用程序(因为我相信子进程无法识别该进程已经完成并正在等待用户关闭应用程序)。 I am running through a loop calling main when a file is found.当找到文件时,我正在运行一个调用 main 的循环。 See below.见下文。
def main(pdffile, printer_name):
# Set the command prompt code
cmd = '"{}" /N /T "{}" "{}"'.format(acrobat, pdffile, printer_name)
# print(cmd)
# Printing the pdf and waiting for process to finish before continuing with the rest of the script
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
proc.communicate('None')[0]
# Get File Name
filename = (os.path.basename(pdffile))
destination_folder = f'{destination_labels}' + filename
# print(destination_folder)
# Move file once printed
shutil.move(pdffile,destination_folder)
Any help is appreciated.任何帮助表示赞赏。
Rather than using subprocess
, I would recommend you to use the default open()
and close()
functions.我建议您使用默认的open()
和close()
函数,而不是使用subprocess
。 Here's a sample code:这是一个示例代码:
from PyPDF2 import PDFFileReader
with open('document_path.PDF','rb') as file: #Automatically closes file when exited.
pdf_file = PDFFileReader(file)
first_page = pdf_file.getPage(0) # Get Page 1
print(first_page.extractText())
To install PyPDF2 simply run pip install pypdf2
in cmd
.要安装 PyPDF2,只需运行pip install pypdf2
in cmd
。
For anyone wondering, the loop hole I used was os.startfile(acrobat)
, which pre launched the adobe application.对于任何想知道的人,我使用的漏洞是os.startfile(acrobat)
,它预启动了 adobe 应用程序。 This meant that when I opened a new instance of adobe using subprocess.popen
, once the file printed it would recognise the file as be completed and carry on with the rest of the script.这意味着当我使用subprocess.popen
打开一个新的 adobe 实例时,一旦文件打印出来,它就会将文件识别为已完成并继续执行脚本的 rest。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.