【问题标题】:Extract file attachment from Outlook VCALENDAR ics event file从 Outlook VCALENDAR ics 事件文件中提取文件附件
【发布时间】:2021-07-21 07:40:10
【问题描述】:

我收到一封带有 ics 活动附件的电子邮件,我将其 imported 输入 Google 日历,但后来发现附件丢失了。该文件约为 3MB,我可以在 ATTACH 组件中看到一个 base64 编码的 docx 文件:

$ cat event.ics | head -n 24
BEGIN:VCALENDAR
PRODID:-//Microsoft Corporation//Outlook 16.0 MIMEDIR//EN
VERSION:2.0
METHOD:REQUEST
X-MS-OLK-FORCEINSPECTOROPEN:TRUE
BEGIN:VTIMEZONE
TZID:Eastern Standard Time
BEGIN:STANDARD
DTSTART:16011104T020000
RRULE:FREQ=YEARLY;BYDAY=1SU;BYMONTH=11
TZOFFSETFROM:-0400
TZOFFSETTO:-0500
END:STANDARD
BEGIN:DAYLIGHT
DTSTART:16010311T020000
RRULE:FREQ=YEARLY;BYDAY=2SU;BYMONTH=3
TZOFFSETFROM:-0500
TZOFFSETTO:-0400
END:DAYLIGHT
END:VTIMEZONE
BEGIN:VEVENT
ATTACH;ENCODING=BASE64;VALUE=BINARY;X-FILENAME="Instructions.docx":UEsDBBQABgA
    AAgCW0NvbnRlbnRfVHlwZXNdLnhtbCCiBAIooAACAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
    AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA

如何在 Python 或 Bash 中提取此附件?

【问题讨论】:

    标签: python base64 icalendar


    【解决方案1】:

    我找到了一个似乎有效的icalendar pypi module

    apt-get install python3-venv
    cd /tmp
    python3 -m venv ical
    source ./ical/bin/activate
    
    pip install icalendar
    
    python << SCRIPTMARKER
    from icalendar import Calendar, Event
    import base64
    import io
    
    g = open('/tmp/event.ics','rb') # read ics
    gcal = Calendar.from_ical(g.read())
    
    for component in gcal.walk():
        print(component.name)
        if component.name == "VEVENT":
            b64 = component.get('attach')
            data = base64.b64decode(b64)
            f = open('/tmp/event.docx','wb') # write binary attachment
            f.write(data)
            print(f)
            f.close()
    
    g.close()
    SCRIPTMARKER
    
    # Results after running the above:
    VCALENDAR
    VTIMEZONE
    STANDARD
    DAYLIGHT
    VEVENT
    <_io.BufferedWriter name='/tmp/event.docx'>
    VALARM
    

    然后我可以将 event.docx 文件上传到 Google Drive 并在那里打开它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-11
      • 1970-01-01
      • 2017-07-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多