Python脚本:自动打印/tmp目录下的PDF文件
使用Python的os和subprocess模块可以轻松实现每分钟自动检查/tmp目录下所有文件夹是否有PDF文件,如果有则调用默认打印机输出内容的脚本。下面是一个简单的例子,支持中文文件名:
import os
import subprocess
import time
def check_pdf_files(directory):
for root, dirs, files in os.walk(directory):
for file in files:
if file.endswith('.pdf'):
return True
return False
def print_pdf(file_path):
try:
subprocess.run(['lpr', file_path], check=True)
print(f'Printed: {file_path}')
except subprocess.CalledProcessError:
print(f'Failed to print: {file_path}')
if __name__ == '__main__':
directory = '/tmp'
while True:
if check_pdf_files(directory):
print_pdf_files(directory)
time.sleep(60)
该脚本会每分钟遍历/tmp目录下的所有文件夹,检查是否有.pdf文件。如果有,它将调用默认打印机打印该文件。你可以根据自己的需求修改目录和打印命令。
原文地址: https://www.cveoy.top/t/topic/nggy 著作权归作者所有。请勿转载和采集!