我正在尝试从电子邮件(.msg)中提取附件( zip ),然后从zip文件中提取唯一的文档(xls)。
提取xls后,我想根据.msg中的关键字对其进行重命名(即,如果包含'Alipay‘,则在xls文件名中附加'_alipay’或'_tng')
import os
import extract_msg
import fnmatch
import zipfile
import glob
Tk().withdraw()
directory = askdirectory(title='Yo select your folder please')
input_dir = directory + "/"
os.chdir(input_dir)
pwd = '123'
keyword = '*Alipay*'
for email in os.listdir(input_dir):
if email.endswith('.msg'):
email_path = os.path.join(input_dir, email)
if fnmatch.fnmatch(email, keyword):
trans_type = '_alipay'
else:
trans_type = '_tng'
msg = extract_msg.Message(email)
msg.save_attachments()
msg.close()
for em_zip in glob.glob('*.zip'):
zip_path = os.path.join(input_dir, em_zip)
with zipfile.ZipFile(zip_path, 'r') as zf:
zf.extractall(pwd=bytes(pwd, 'utf-8'))
os.rename(zip_path, os.path.splitext(zip_path)[0] + trans_type + '.xls')我得到的错误信息是
回溯(最近一次调用):"C:\Users\cheeh\Desktop\PyCharmPortable\PycharmProjects\ocrpdf\Alipay.py",'C:/Users/cheeh/Desktop/cimb/CDFSB60006039760617520201221_alipay.xls‘第71行,在os.rename(zip_path,os.path.splitext(zip_path) + trans_type + '.xls') PermissionError: WinError 32由于该文件正被另一个进程使用,因此进程无法访问该文件:'C:/Users/cheeh/Desktop/cimb/CDFSB60006039760617520201221.zip’-> Traceback
谢谢
发布于 2021-03-27 15:11:46
我去了一个手册和更长的方式来实现结果,但如果任何专业人士可以优化代码,因为它需要大量的时间,如果有大量的电子邮件,我将不胜感激
keyword = '*Alipay*'
os.makedirs(os.path.join(input_dir, '_Alipay'))
os.makedirs(os.path.join(input_dir, '_TnG'))
for email in glob.iglob('*.msg'):
email_path = os.path.join(input_dir, email)
if fnmatch.fnmatch(email_path, keyword):
trans_type = '_Alipay'
else:
trans_type = '_TnG'
shutil.move(email_path, os.path.join(input_dir, trans_type))
for emails in glob.iglob('*/*.msg', recursive=True):
emails_path = os.path.join(input_dir, emails)
if fnmatch.fnmatch(emails_path, keyword):
trans_type = '_Alipay'
else:
trans_type = '_TnG'
msg = extract_msg.Message(emails_path)
msg.save_attachments(customPath=os.path.join(input_dir, trans_type))
msg.close()
for zips in glob.glob('*/*.zip', recursive=True):
zips_path = os.path.join(input_dir, zips)
if fnmatch.fnmatch(zips_path, keyword):
trans_type = '_Alipay'
else:
trans_type = '_TnG'
dst_folder = os.path.join(input_dir, trans_type)
with zipfile.ZipFile(zips, 'r') as zf:
zf.extractall(path=dst_folder, pwd=bytes(pwd, 'utf-8'))
for xls in glob.glob('*/*.xls', recursive=True):
xls_path = os.path.join(input_dir, xls)
if fnmatch.fnmatch(xls_path, keyword):
trans_type = '_Alipay'
else:
trans_type = '_TnG'
os.rename(xls_path, os.path.splitext(xls_path)[0] + trans_type + '.xls')
for xls in glob.glob('*/*.xls', recursive=True):
xls_path = os.path.join(input_dir, xls)
if fnmatch.fnmatch(xls_path, keyword):
trans_type = '_Alipay'
else:
trans_type = '_TnG'
XLS2XLSX(xls_path).to_xlsx(os.path.splitext(xls_path)[0] + '.xlsx')
for xlsx in glob.glob('*/*.xlsx', recursive=True):
shutil.move(xlsx, input_dir)发布于 2021-03-26 19:20:46
您正在重命名仍然打开的文件,请尝试取消缩进os.rename行,以便在重命名文件之前关闭资源。
with zipfile.ZipFile(zip_path, 'r') as zf:
zf.extractall(pwd=bytes(pwd, 'utf-8'))
os.rename(zip_path, os.path.splitext(zip_path)[0] + trans_type + '.xls')https://stackoverflow.com/questions/66815656
复制相似问题