[Python] 纯文本查看 复制代码
import re
import 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=[ls[x:x+15] 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=[osp.join(path1,s) for s in lg if patt.findall(s)] #获取文件夹下的文件名,并拼接完整路径。
for filename in files:
win32api.ShellExecute(0,"print",filename,None,".",0 )
time.sleep(5)
print(osp.basename(filename)+"已打印")