简体   繁体   English

在 python 中打印后如何关闭 pdf 文件?

[英]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.

相关问题 您如何确保每个唯一的行在python中打印一次 - How do you ensure that each unique line printed once in python 一旦有了我需要的数据,如何关闭Python 2.5.2 Popen子进程? - How do I close a Python 2.5.2 Popen subprocess once I have the data I need? 如何让我的 python 程序在某个时间间隔内只生成一次质数,而不是多次打印它们? - How do I make my python program generate the prime numbers in a certain interval just once instead of them being printed multiple times? python imaplib:我只需要调用一次.close()吗? - python imaplib: Do I have to call .close() only once? Python:如何一次浏览文件中的多行? - Python: How do I go through multiple lines in a file at once? 在 python 中关闭应用程序后如何运行命令? - How can i run a command once i close an app in python? 如何在python中更改特定打印字母的颜色? - How do I change the color of a specific printed letter in python? 如何删除 Python Tkinter 中的打印文本? - How do I delete the printed text in Python Tkinter? 如何通过python保存Google pdf文件? - How do I save a Google pdf file through python? 如何使用 python 将控制台中的打印行附加到 QPlainTextEdit 中? - How do I append printed lines from console into a QPlainTextEdit with python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM