0

大家下午好。这是我在 StackOverflow 上的第一篇文章,所以如果我错过了一些信息或违反任何规则,请宽恕。

我正在尝试以def类的形式在 Python 中编写一个脚本,该脚本打印列表中所有 PDF 文件的硬拷贝(每个 PDF 都是具有其绝对路径的字符串),并且在每个打印作业完成后,执行某些命令(等待几秒钟并播放声音)。

换句话说,理想情况下是这样的:

import win32print
import win32api
import time

# The following is placeholder import of a function I've already created and tested
from view_controller import play_sound

def print_PDFs(files): 
    # <files> is a list of strings. Each string is the absolute path of a single PDF.
    # if instead is passed a string, that is converted into a list with a single string.
    if type(files) is not list:
        files = [files]

    for file in files:
        # Add <file> to the printing queue and start printing

        # When the job is complete, therefore all physical pages are printed,
        play_sound('complete')
        print('Next job in 20 seconds...')
        sleep(20)

让我给你一些背景信息,以了解我想要实现的目标。

这将是一个用 Python 编写的更大程序的一部分,它根据给定的标准生成一堆 PDF 文件(我不会在这里进一步挖掘,没关系);然后我的想法是播放声音来警告我它已经完成,让打印机在传递到下一个之前等待 20 秒,这样我就有时间把我在那一刻在办公室做的事情放在一边,然后装订已经完成的事情刚出来打印,最后继续在list.

我尝试了很多解决方案,使用了 win32print 和 win32api、ShellExecutable,基本上所有在线上的东西,以及我可以自己发明的所有东西。但在我看来,Windows API 非常古怪。这就是我尝试过的: https ://www.blog.pythonlibrary.org/2013/12/19/pywin32-monitor-print-queue/ ,当然还有: http: //timgolden.me.uk/pywin32-docs/ win32print.html

我设法创建了一个打印列表中所有文件的脚本,它工作正常;对我来说困难的是 a) 完成后从打印机了解;b) 确保它自己运行;有时 Tim Golden 脚本会弹出 Adob​​e Acrobat Reader 并要求用户确认……在现实生活中,这意味着在我不知道的情况下停止程序运行。

另一件事:它必须在 Windows 上运行,这就是他们在我们办公室的操作系统。打印机是网络打印机,但我想调整任何脚本来适应使用 win32api 的脚本并不难。

感谢您提供的任何建议或灵感,我对此感到非常孤单!

4

0 回答 0