2017-05-04 3 views
2

.msg 파일이 많은 폴더가 있습니다. 첨부 파일 중 하나를 저장할 수 있기를 원합니다. 내 생각은 파일을 클릭 한 다음 특정 파일 이름으로 파일을 추출하는 작업을 자동화하는 것이지만 아직 어떤 해결책도 찾지 못했습니다.Python : .msg 파일의 첨부 파일 저장

어떻게해야합니까? 아니면 더 좋은 방법일까요?

감사합니다.

업데이트 : os.startfile을 사용하여 파일을 열고 싶습니다. 어떻게 다른 창에서 열지 않습니까? 하지만 그랬던 것처럼 행동합니까? 그게 말이되는 경우 :/

+0

이 질문은 당신에게 도움이 될 수 있습니다 : 당신이 폴더가 있다고 가정하기 때문에

import win32com.client outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI") msg = outlook.OpenSharedItem(filename) #filename including path att=msg.Attachments for i in att: i.SaveAsFile(os.path.join(Pathname, i.FileName))#Saves the file with the attachment name 

, 이것은 전체 폴더를 자동화합니다 HTTP ://stackoverflow.com/questions/9937664/how-to-extract-attachments-from-msg-files – Sevyns

+0

이 파일을 보았습니다! 불행히도 msg 파일은 이미 폴더에 있습니다. 따라서 Outlook과 직접 상호 작용하지는 않습니다. – arthur6523

답변

1

문제가 해결 될지는 모르겠지만 파이썬은 email.parser과 함께 제공됩니다. 이것은 적어도 당신에게 Message 객체를 줄 것이다

import email 
with open('/path/to/your/file.msg') as fl: 
    msg = email.message_from_file(fl) 

(이 적절한 형식의 가정) 당신은 MSG 파일을 읽을 도움이 될 것입니다. 첨부 된 모든 콘텐츠를 제공하는 msg.walk()을 사용하여 파일을 가져올 수 있어야합니다.

for part in msg.walk(): 
    if part.get_content_type() == "image/png": 
     with open('out.png', 'w') as fl: 
      fl.write(part.get_payload(decode=True))) 
0

이 작동합니다 :

import win32com.client 
import os 
files = [f for f in os.listdir('.') if os.path.isfile(f)] 
for file in files: 
    if file.endswith(".msg"): 
     outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI") 
     msg = outlook.OpenSharedItem(file)   
     att=msg.Attachments 
     for i in att: 
      i.SaveAsFile(os.path.join(Pathname, i.FileName))#Saves the file with the attachment name