Skip to content

Instantly share code, notes, and snippets.

@akilab
Created October 29, 2018 05:41
Show Gist options
  • Select an option

  • Save akilab/54bce8addfecc60af3614550b5545675 to your computer and use it in GitHub Desktop.

Select an option

Save akilab/54bce8addfecc60af3614550b5545675 to your computer and use it in GitHub Desktop.
PythonのOutlook添付ファイルをメモリ内でごにょごにょするために調査した結果
# coding: utf-8
"""
Anaconda環境を想定
参考URL:
https://docs.microsoft.com/en-us/office/vba/api/outlook.olattachmenttype
https://docs.microsoft.com/en-us/office/vba/outlook/concepts/attachments/modify-an-attachment-of-an-outlook-email-message
https://qiita.com/pashango2/items/5075cb2d9248c7d3b5d4#memoryview%E3%81%A7bytes%E3%81%AB%E5%A4%89%E6%8F%9B
"""
import win32com.client
import os.path
import pythoncom
import hashlib
PR_ATTACH_DATA_BIN = "http://schemas.microsoft.com/mapi/proptag/0x37010102"
pythoncom.CoInitialize() # 不要な環境もある
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
inbox = outlook.GetDefaultFolder(6)
folders = inbox.Folders
dev_folder = folders("TEST_FOLDER")
save_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "output")
for mailObj in dev_folder.Items:
for attachment in mailObj.attachments:
name = attachment.FileName
"""
Name Value Description
olByReference 4 This value is no longer supported since Microsoft Outlook 2007. Use olByValue to attach a copy of a file in the file system.
olByValue 1 The attachment is a copy of the original file and can be accessed even if the original file is removed.
olEmbeddeditem 5 The attachment is an Outlook message format file (.msg) and is a copy of the original message.
olOLE 6 The attachment is an OLE document.
"""
if attachment.Type == 1:
bin_data = attachment.PropertyAccessor.GetProperty(PR_ATTACH_DATA_BIN)
b = bin_data.tobytes()
print(hashlib.md5(b).hexdigest())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment