python - 从 Outlook VCALENDAR ics 事件文件中提取文件附件
问题描述
我收到一封带有 ics 事件附件的电子邮件,我将其导入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 中提取此附件?
解决方案
我发现了一个似乎可以工作的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 并在那里打开它。
推荐阅读
- python - 如何在管理员中查看 models.ManyToManyField('self')?
- android - RxJava 替代 CompletableFuture
- javascript - 插件不适用于第二张图片,Javascript
- python - Python3 __pycache__ 即使 PYTHONDONTWRITEBYTECODE=1 也会生成
- python - python的控制流语句的性能
- asp.net-mvc - MVC 查看图像拖放功能
- python - 在 Zapier Code 中返回未解析的字典列表
- hyperledger-fabric - 作曲家休息服务器中的查询过滤器
- wordpress - 管理区域是否可以有一个非标准页面?
- r - par 中的错误,未找到对象 true