从outlookvCalendarICS事件文件中提取文件附件

cld4siwp  于 2021-07-13  发布在  Java
关注(0)|答案(1)|浏览(351)

我收到了一封带有ics事件附件的电子邮件,我把它导入了google日历,但后来发现它丢失了附件。文件是~3mb,我可以在附加组件中看到一个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中提取此附件?

cl25kdpy

cl25kdpy1#

我发现了一个icalendar pypi模块,它似乎可以工作:

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并在那里打开它。

相关问题