python - 使用 Python 通过子文件夹中的消息保存 Outlook 中的附件
问题描述
我想使用以下结构从 Outlook 中保存 Outlook 附件:
附加(在桌面上)--> 文件夹(Outlook 中子文件夹的名称)--> 文件夹(单个邮件)--> 邮件中的附件
我遇到了一个问题,由于某种原因,子文件夹中的所有附件都保存到每个邮件文件夹中,而不仅仅是它们所属的邮件文件夹。
对不起,如果我的代码是多余的,我刚刚开始学习!
import win32com.client as client
import os
outlook = client.Dispatch('Outlook.Application').GetNamespace('MAPI')
path = 'C:\\Users\\XXXXX\\Desktop\\Attach'
os.chdir(path)
inbox = outlook.GetDefaultFolder(6)
inboxfolders = inbox.Folders
inbxcount = inboxfolders.Count
folderlist = []
for x in range(1, inbxcount + 1):
subfolder = inboxfolders[f'{x}']
strsub = str(subfolder)
if 'PRs' in strsub:
folderlist.append(strsub)
for i in folderlist:
newfolder = i
os.makedirs(newfolder)
sub = inboxfolders[f'{i}']
messages = sub.Items
msgcount = messages.Count
newpath = f'{path}\\{newfolder}'
for y in range(1, msgcount + 1):
msgfolder = f'PR{y}'
os.chdir(newpath)
os.makedirs(msgfolder)
path3 = f'{newpath}\\PR{y}'
for msg in range(1, msgcount + 1):
message = messages.Item(msg)
attachments = message.Attachments
attchcount = attachments.Count
for attch in range(1, attchcount + 1):
attachment = attachments.Item(attch)
sattachment = str(attachment)
if 'image' not in sattachment:
attachment.SaveAsFile(os.path.join(path3, sattachment))
os.chdir(path)
解决方案
解决方案是删除对单个消息进行迭代的第二个 for 循环。通过这样做,它消除了额外的附件。
推荐阅读
- java - 正则表达式否定模式
- ios - Swift - WKWebKit - 无法在外部浏览器(Safari)中打开链接
- javascript - Javascript/JQuery 如果 cookie 不存在 接受
- wordpress - 带有 wp_list_comments() 的 Wordpress 评论顺序
- automationanywhere - Web 元素上的“设置文本”操作间歇性地生成 TYPE_021 错误
- python - 如何对熊猫中的两组列执行元素明智的操作
- data-structures - 处理大型数组上的大量查询的数据结构
- python - Python Enthought Canopy Word Scrambler
- jquery - 当单击另一个 div 时,jquery 更改 div 的类和内联样式
- jquery - 未在隐藏输入值上调用的函数已更改 Jquery