赞
踩
python连接电脑默认打印机,然后批量打印指定文件夹的所有文件,还带参数设置
#!/usr/bin/env python
import os
import win32print
def print_files_in_folder(folder_path, double_sided=False, copies=1,color=False):
printer_name = win32print.GetDefaultPrinter()
hPrinter = win32print.OpenPrinter(printer_name)
try: for file_name in os.listdir(folder_path): file_path = os.path.join(folder_path, file_name) with open(file_path, 'rb') as f: file_contents = f.read() # 设置打印参数 print_flags = 0 if double_sided: print_flags |= 2 # 设置为双面打印 if copies > 1: print_flags |= 1 # 设置为多份打印 if color: print_flags |= 16384 # 设置为彩色打印 # 开始打印 win32print.StartDocPrinter(hPrinter, 1,(file_name, None, 'RAW')) try: win32print.StartPagePrinter(hPrinter) win32print.WritePrinter(hPrinter, file_contents) win32print.EndPagePrinter(hPrinter) print(f"文件 '{file_name}' 已成功打印:{'双面' if double_sided else '单面'},{copies}份,{'彩色' if color else '黑白'}打印") finally: win32print.EndDocPrinter(hPrinter) finally: win32print.ClosePrinter(hPrinter)
if name == “main”:
# 调用打印函数并传入文件路径和打印选项
file_path = r’C:\Users\XX’ #请换成你需要打印的文件所在的文件夹路径
print_files_in_folder(file_path, double_sided=True, copies=1,color=False) #请根据自己的需要设置打印的参数
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。