You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python中打印PDF后如何自动关闭Acrobat应用程序?

解决Acrobat打印PDF后无法自动关闭的问题

原因分析

你当前使用的/T参数会让Acrobat在发送打印任务后保持打开状态,导致subprocess.communicate()一直等待进程退出,必须手动关闭程序才能继续执行后续逻辑。

解决方案

方案1:调整Acrobat命令行参数,实现打印后自动退出

Acrobat的命令行参数支持更完整的打印格式,补充驱动和端口的空参数(部分版本需要),或替换为/p参数,可触发打印后自动退出:

  • 补充完整/T参数格式:
    cmd = '"{}" /N /T "{}" "{}" "" ""'.format(acrobat, pdffile, printer_name)
    
  • 尝试/p静默打印参数(部分Acrobat/Reader版本适用):
    cmd = '"{}" /N /p "{}" "{}"'.format(acrobat, pdffile, printer_name)
    

方案2:手动终止目标Acrobat进程

如果参数调整无效,可在确认打印完成后,强制终止当前启动的Acrobat进程(需安装psutil库:pip install psutil):

import os
import subprocess
import shutil
import psutil

def terminate_proc_tree(pid):
    """终止进程及其所有子进程"""
    try:
        parent = psutil.Process(pid)
        for child in parent.children(recursive=True):
            child.terminate()
        parent.terminate()
        parent.wait()
    except Exception as e:
        print(f"终止进程失败: {e}")

def main(pdffile, printer_name):
    cmd = '"{}" /N /T "{}" "{}"'.format(acrobat, pdffile, printer_name)
    proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    
    # 根据实际打印速度设置等待时间,或结合打印队列监控(见方案3)
    import time
    time.sleep(8)  # 等待打印任务完成
    
    # 终止当前启动的Acrobat进程树
    terminate_proc_tree(proc.pid)
    
    # 移动打印完成的文件
    filename = os.path.basename(pdffile)
    destination_folder = f'{destination_labels}{filename}'
    shutil.move(pdffile, destination_folder)

方案3:监控打印队列,精准触发进程关闭

通过Windows打印队列API确认任务完成后再终止进程(需安装pywin32库:pip install pywin32),避免固定等待时间的不确定性:

import os
import subprocess
import shutil
import psutil
import win32print
import time

def terminate_proc_tree(pid):
    try:
        parent = psutil.Process(pid)
        for child in parent.children(recursive=True):
            child.terminate()
        parent.terminate()
        parent.wait()
    except Exception as e:
        print(f"终止进程失败: {e}")

def is_print_job_completed(printer_name, job_doc_name):
    """检查指定文档的打印任务是否完成"""
    printer_handle = win32print.OpenPrinter(printer_name)
    try:
        # 获取所有未完成的打印任务
        jobs = win32print.EnumJobs(printer_handle, 0, -1, 1)
        for job in jobs:
            if job["pDocument"] == job_doc_name:
                return False
        return True
    finally:
        win32print.ClosePrinter(printer_handle)

def main(pdffile, printer_name):
    cmd = '"{}" /N /T "{}" "{}"'.format(acrobat, pdffile, printer_name)
    proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    
    job_doc_name = os.path.basename(pdffile)
    # 循环等待打印任务完成
    while not is_print_job_completed(printer_name, job_doc_name):
        time.sleep(2)
    
    # 终止Acrobat进程
    terminate_proc_tree(proc.pid)
    
    # 移动文件
    filename = os.path.basename(pdffile)
    destination_folder = f'{destination_labels}{filename}'
    shutil.move(pdffile, destination_folder)

内容的提问来源于stack exchange,提问作者diinkantr-bw

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.17 12:15:45