python批量打印excel文件
import reimport os
import os.path as osp
import win32print
import tempfile
import win32api
import shutil
import time
reg=re.compile(r'^\d+')#正则表达式
path1=r'G:\桌面\新建文件夹\打印1'
ls=['32657-华为T园区3号楼-PTN7900-SPEH1','32665-华为T园区4号楼-PTN7900-SPEH1','12033-大朗1扩壹-PTN7900-SPNH1','12041-大朗4扩壹-PTN7900-SPNH1','16161-樟木头1扩壹-PTN7900-SPNH1',
'16169-樟木头4-PTN7900-SPNH1','16201-凤岗7-PTN7900-SPNH1','20233_SPNH5/1_常平2_7900E-12','3953-万江3-PTN7900-SPNH1','3961-万江4-PTN7900-SPNH1','7937_SPNH2/1_寮步3_7900E-12',
'3881_SPNH1/2_柏洲边_7900E-24','8017-石排2-SPNH1-7900E','8009-石排4-SPNH1-7900E','20321-横沥6-PTN7900-SPNH1','20264-横沥2-PTN7900-SPNH1','20225-常平第三机房-PTN7900-SPNH1',
'120249-常平第八机房-PTN7900-SPNH1','108025-大朗第三机房-PTN7900-REG石排-SPNH1','6097_大朗PTNH10_大朗1扩壹_PTN7900','6113_大朗PTNH10_大朗2_PTN7900','10161_常平PTNH10_常平4_PTN7900',
'10145_常平PTNH10_常平3_PTN7900','20265-横沥2-PTN7900-SPNH1.1','20257-横沥4-PTN7900-SPNH1','3865_SPNH1/1_牛山_7900E-24','20361-常平第二传输机房-PTN7900-企石REG-SPNH1',
'20369-常平第二传输机房-PTN7900-谢岗REG-SPNH1','5265_城区PTNH26_篁胜扩容壹_PTN7900','19777_城区PTNH31_柏洲边_PTN7900','19761_城区PTNH31_温塘二市_PTN7900','16177-清溪3-PTN7900-SPNH1',
'16185-清溪4-PTN7900-SPNH1','20289-桥头3-PTN7900-SPNH1','20297-桥头2扩壹-PTN7900-SPNH1','16153-塘厦第二传输机房六楼-PTN7900-SPNH1','16129-塘厦镇第五传输机房七楼-PTN7900-SPNH1',
'12113-东坑4-PTN7900-SPNH1','12121-东坑5-PTN7900E-东坑SPNH汇聚环1','26417-东城4-PTN7900E-东城SPNH汇聚环2','26385-蓢基湖-PTN7900-SPNH2','26401-寮步2扩壹-PTN7900-SPNH2','26409-寮步5-PTN7900-SPNH2',
'20345-桥头4-PTN7900-SPNH1','20353-桥头5-PTN7900-SPNH1','8065_寮步PTNH12_寮步3_PTN7900','8081_寮步PTNH12_寮步4_PTN7900','43010-麻涌2-PTN6700-SPNZ1','143011-麻涌1扩壹-PTN6700-麻涌SPNZ汇聚1',
'42498-中堂1扩壹-PTN6700-SPNZ1','42499-中堂4-PTN6700-SPN1','17665_桥头PTNH1_桥头1扩壹PTN3900','17666-桥头2-PTN3900-桥头1']
lss= for x in range(0,len(ls),15)]
lt=[]
for i in ls:
t=reg.search(i).group()#正则表达式提取
lt.append(t)
lg=os.listdir(path1)#带有id号开头的名字很长的xlsx文件列表
lg.sort(key=lambda x: lt.index(reg.search(x).group()))#对文件名以列表里的id号的顺序进行排序,特别关键的步骤
patt=re.compile(r"(.xlsx)$")
files=#获取文件夹下的文件名,并拼接完整路径。
for filename in files:
win32api.ShellExecute(0,"print",filename,None,".",0 )
time.sleep(5)
print(osp.basename(filename)+"已打印")
要是能做成成品,更好了 不错。可以优化一下:
import tkinter as tk
from tkinter import filedialog
import win32api
import time
def print_files():
file_paths = filedialog.askopenfilenames(filetypes=[
("Word Files", "*.docx"),
("Excel Files", "*.xlsx"),
("PDF Files", "*.pdf"),
("Image Files", "*.png;*.jpeg;*.jpg"),
("Text Files", "*.txt"),
("All Files", "*.*")
])
for file_path in file_paths:
win32api.ShellExecute(0, "print", file_path, None, ".", 0)
time.sleep(5)
printed_files_text.insert(tk.END, file_path + "\n")
print(f"{file_path} 已打印")
def create_gui():
global printed_files_text
root = tk.Tk()
root.title("批量打印工具")
print_button = tk.Button(root, text="选择并打印文件", command=print_files)
print_button.pack(pady=10)
printed_files_label = tk.Label(root, text="已打印的文件:")
printed_files_label.pack(pady=5)
printed_files_text = tk.Text(root, wrap=tk.WORD, width=50, height=10)
printed_files_text.pack(pady=5)
root.mainloop()
if __name__ == "__main__":
create_gui()
能不能修改一下,批量打印word文件 牛啊,批量打印省下了不少时间,远比现在一个文件一个文件打印来的舒服 这个很实用 yu520 发表于 2023-8-12 11:04
能不能修改一下,批量打印word文件
把第34行里的.xlsx改成.doc就行 牛逼,这个好实用 这个牛逼啊,非常感谢,很有用 yu520 发表于 2023-8-12 11:04
能不能修改一下,批量打印word文件
有铁子告诉你了 fishyou 发表于 2023-8-12 11:57
要是能做成成品,更好了
源码不是更香么?