使用Pythin脚本从Outlook下载附件

时间:2019-11-03 10:03:47

标签: python outlook download attachment

现在,我编写了一个代码,可以每天运行该代码以获取昨天电子邮件的附件。 当我运行它时,附件没有下载,而是得到了一个名为“ @”的文件。

下面是代码:

import win32com.client
import os
from datetime import datetime, date, time, timedelta

outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
inbox = outlook.GetDefaultFolder(6).Folders.Item("Inbox Subfolder Name") 
messages = inbox.Items
message = messages.GetFirst()
subject = message.Subject

# Get yesterdays date for the purpose of getting emails from this date
d = (date.today() - timedelta (days=1)).strftime("%y-%m-%d")
yesterdaydate=("20"+d)
print yesterdaydate

get_path_clt1 = "Folder Location" + yesterdaydate+"\\"

if os.path.exists(get_path_clt1):
    print " File Exists"
else:
    os.mkdir(get_path_clt1)


for m in messages:
    date = str(m.senton)
    date_time_obj = datetime.strptime(date,'%m/%d/%y %H:%M:%S')
    messagedate=date_time_obj.date()

    if str(yesterdaydate) == str(messagedate) and (m.Subject == "Subject Name to compare") :
        print (message)
        print (messagedate)
        attachments = message.Attachments
        num_attach = len([x for x in attachments])
        print("test-1")
        for x in range(1, num_attach+1):
            print("test-in-1")
            attachment = attachments.Item(x)
            attachment.SaveAsFile(os.path.join(get_path_clt1,attachment.FileName))
            print (attachment)
            print ("---------------------")
            break
        message = messages.GetNext()

    else:
        message = messages.GetNext()

1 个答案:

答案 0 :(得分:0)

首先,不需要遍历所有附件来获取号码:

<div class = "description">
    A 3 x2 texture <br />
</div>
<canvas id = "canvas"></canvas>

    <!-- vertex shader -->
    <script id = "3d-vertex-shader" type = "x-shader/x-vertex">
        attribute vec4 a_position;
        attribute vec2 a_texcoord;
        uniform mat4 u_matrix;
        varying vec2 v_texcoord;
        void main()
        {
            // Multiply the position by the matrix.
            gl_Position = u_matrix * a_position;
            // Pass the texcoord to the fragment shader.
            v_texcoord = a_texcoord;
        }
    </script>
    <!-- fragment shader -->
    <script id = "3d-fragment-shader" type = "x-shader/x-fragment">
        precision mediump float;
        // Passed in from the vertex shader.
        varying vec2 v_texcoord;
        // The texture.
        uniform sampler2D u_texture;
        void main()
        {
            gl_FragColor = texture2D(u_texture, v_texcoord);
        }
    </script>
    <!--skybox vertex shader-->
    <script id="skybox-vertex-shader" type="x-shader/x-vertex">
        attribute vec4 a_position;
        varying vec4 v_position;
        void main() 
        {
          v_position = a_position;
          gl_Position = a_position;
        }
    </script>
    <!--skybox fragment shader-->
    <script id="skybox-fragment-shader" type="x-shader/x-fragment">
        precision mediump float;
        uniform samplerCube u_skybox;
        uniform mat4 u_viewDirectionProjectionInverse;
        varying vec4 v_position;
        void main() 
        {
          vec4 t = u_viewDirectionProjectionInverse * v_position;
          gl_FragColor = textureCube(u_skybox, normalize(t.xyz / t.w));
        }
    </script>
<script src="https://webglfundamentals.org/webgl/resources/webgl-utils.js"></script>
<script src="https://webglfundamentals.org/webgl/resources/m4.js"></script>
<script src="https://webglfundamentals.org/webgl/resources/primitives.js"></script>

相反,您可以使用Attachments.Count属性,该属性返回一个整数,该整数指示指定集合中的对象数。

第二,确保您指定了有效的文件名:

 num_attach = len([x for x in attachments])

我建议您先检查 attachment.SaveAsFile(os.path.join(get_path_clt1,attachment.FileName)) 的值或生成自己的值。

最后,您可以检查Attachment.Type属性,该属性返回一个attachment.FileName常量,该常量指示指定对象的类型。