{"id":535,"date":"2020-04-26T07:52:40","date_gmt":"2020-04-26T07:52:40","guid":{"rendered":"http:\/\/datadigging.ru\/?p=535"},"modified":"2021-05-18T13:55:51","modified_gmt":"2021-05-18T10:55:51","slug":"%d0%bf%d0%b0%d1%80%d1%81%d0%b8%d0%bd%d0%b3-%d0%bf%d0%b8%d1%81%d0%b5%d0%bc-%d0%b8%d0%b7-outlook-%d0%b2-python","status":"publish","type":"post","link":"https:\/\/datadigging.ru\/?p=535","title":{"rendered":"\u041f\u0430\u0440\u0441\u0438\u043d\u0433 \u043f\u0438\u0441\u0435\u043c \u0438\u0437 Outlook \u0432 Python"},"content":{"rendered":"\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-flow wp-block-group-is-layout-flow\">\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-flow wp-block-group-is-layout-flow\">\n<p class=\"wp-block-paragraph\"><em>Python-c\u043a\u0440\u0438\u043f\u0442 \u0440\u0430\u0437\u0431\u043e\u0440\u0430 \u043f\u0438\u0441\u0435\u043c \u0438\u0437 \u043f\u0430\u043f\u043a\u0438 \"\u0412\u0445\u043e\u0434\u044f\u0449\u0438\u0435\" Outlook. \u0421\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u0435 \u043f\u0438\u0441\u044c\u043c\u0430 \u043a\u0430\u043a html-\u0441\u0442\u0440\u0430\u043d\u0438\u0446\u044b \u0441 \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u0435\u043c \u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f \u0432\u043b\u043e\u0436\u0435\u043d\u043d\u044b\u0445 \u043a\u0430\u0440\u0442\u0438\u043d\u043e\u043a<\/em><\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code>import win32com.client\nimport os\nimport datetime\nimport re\n\noutlook = win32com.client.Dispatch(\"Outlook.Application\").GetNamespace(\"MAPI\")\ninbox = outlook.GetDefaultFolder(6).Items  # 6- \u043f\u0430\u043f\u043a\u0430 \u0412\u0445\u043e\u0434\u044f\u0449\u0438\u0435 Outlook\nmsg = inbox.GetLast() # \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u0435 \u043f\u0438\u0441\u044c\u043c\u043e \u0432 \u044f\u0449\u0438\u043a\u0435\n\npath  = r'C:\\Users\\user\\Documents' # \u043f\u0430\u043f\u043a\u0430 \u0434\u043b\u044f \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0432\u043b\u043e\u0436\u0435\u043d\u0438\u0439 \u043f\u0438\u0441\u044c\u043c\u0430\n\nwhile msg :\n    subject = str(msg.Subject) # \u0442\u0435\u043c\u0430 \u043f\u0438\u0441\u044c\u043c\u0430\n    msg_date = datetime.datetime.strptime(str(msg.SentOn)&#91;0:19], '%Y-%m-%d %H:%M:%S')\n    to_list = str(msg.To).split(';') # \u0441\u043f\u0438\u0441\u043e\u043a \u043f\u043e\u043b\u0443\u0447\u0430\u0442\u0435\u043b\u0435\u0439\n    sender = msg.SenderName # \u043e\u0442\u043f\u0440\u0430\u0432\u0438\u0442\u0435\u043b\u044c\n    text = str(msg.Body) # \u0442\u0435\u043a\u0441\u0442 \u043f\u0438\u0441\u044c\u043c\u0430\n    html_text = str(msg.HTMLBody) # html \u043a\u043e\u0434 \u043f\u0438\u0441\u044c\u043c\u0430\n    \n    # \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u0435 \u0432\u043b\u043e\u0436\u0435\u043d\u0438\u0439\n    att_list=&#91;]\n    for att in msg.Attachments:\n        att_name = att.FileName\n        att.SaveAsFile(path + '\\\\' + att_name) # \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u0435 \u0444\u0430\u0439\u043b\u0430 \u0432\u043b\u043e\u0436\u0435\u043d\u0438\u044f\n        att_list.append(att_name)  \n    \n    # \u043a\u043e\u0440\u0440\u0435\u043a\u0442\u0438\u0440\u043e\u0432\u043a\u0430 html-\u043a\u043e\u0434\u0430 \u0434\u043b\u044f \u043e\u0442\u0440\u0430\u0436\u0435\u043d\u0438\u044f \u0432\u043b\u043e\u0436\u0435\u043d\u043d\u044b\u0445 \u043a\u0430\u0440\u0442\u0438\u043d\u043e\u043a (\u0437\u0430\u043c\u0435\u043d\u0430 \u0430\u0434\u0440\u0435\u0441\u0430 \u043a\u0430\u0440\u0442\u0438\u043d\u043e\u043a \u043d\u0430 \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u044b\u0439 \u0432\u0430\u0440\u0438\u0430\u043d\u0442)\n    if att_list:\n        for fname in att_list:\n            # \u043f\u0430\u0442\u0442\u0435\u0440\u043d \u0441\u0441\u044b\u043b\u043a\u0438 \u043d\u0430 \u0432\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u043d\u043e\u0435 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435\n            pattern = '\\\"cid:' + fname  + '@&#91;\\w]{,20}.&#91;\\w]{,20}\\\"'\n            html_text = re.sub(pattern, '\\\"' + fname + '\\\"', html_text, count=5)\n    \n    # \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435  html-\u0441\u0442\u0440\u0430\u043d\u0438\u0446\u044b \u0441 \u0442\u0435\u043b\u043e\u043c \u043f\u0438\u0441\u044c\u043c\u0430\n    with open(path + '\\\\' + 'index_.html', 'w', encoding='utf8') as file:\n        file.write(html_text)\n    \n    msg = inbox.GetPrevious() # \u043f\u0435\u0440\u0435\u0445\u043e\u0434 \u043a \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c\u0443 \u043f\u0438\u0441\u044c\u043c\u0443\n<\/code><\/pre>\n<\/div><\/div>\n<\/div><\/div>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python-c\u043a\u0440\u0438\u043f\u0442 \u0440\u0430\u0437\u0431\u043e\u0440\u0430 \u043f\u0438\u0441\u0435\u043c \u0438\u0437 \u043f\u0430\u043f\u043a\u0438 &#8220;\u0412\u0445\u043e\u0434\u044f\u0449\u0438\u0435&#8221; Outlook. \u0421\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u0435 \u043f\u0438\u0441\u044c\u043c\u0430 \u043a\u0430\u043a html-\u0441\u0442\u0440\u0430\u043d\u0438\u0446\u044b \u0441 \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u0435\u043c \u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f \u0432\u043b\u043e\u0436\u0435\u043d\u043d\u044b\u0445 \u043a\u0430\u0440\u0442\u0438\u043d\u043e\u043a<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_exactmetrics_skip_tracking":false,"footnotes":""},"categories":[1],"tags":[78,9,57,16,15,77],"class_list":["post-535","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-outlook","tag-python","tag-win32com","tag-16","tag-15","tag-77"],"_links":{"self":[{"href":"https:\/\/datadigging.ru\/index.php?rest_route=\/wp\/v2\/posts\/535","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/datadigging.ru\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/datadigging.ru\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/datadigging.ru\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/datadigging.ru\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=535"}],"version-history":[{"count":5,"href":"https:\/\/datadigging.ru\/index.php?rest_route=\/wp\/v2\/posts\/535\/revisions"}],"predecessor-version":[{"id":667,"href":"https:\/\/datadigging.ru\/index.php?rest_route=\/wp\/v2\/posts\/535\/revisions\/667"}],"wp:attachment":[{"href":"https:\/\/datadigging.ru\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=535"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/datadigging.ru\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=535"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/datadigging.ru\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=535"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}