我正在尝试解析一个.eml文件。该文件包含一个当前为base 64编码的Excel附件。我正在努力弄清如何将其解码为XML,以便稍后将其转换为可以处理的CSV。
这是我现在的代码:
这是我现在的代码:
import email
data = file('Openworkorders.eml').read()
msg = email.message_from_string(data)
for part in msg.walk():
c_type = part.get_content_type()
c_disp = part.get('Content Disposition')
if part.get_content_type() == 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet':
excelContents = part.get_payload(decode = True)
print excelContents
问题是
当我尝试解码时,它会返回类似于这样的内容。
我使用这篇文章帮助我编写上面的代码。
更新:
这完全按照文章的解决方案与我的文件一起使用,但part.get_payload()
仍然返回所有编码内容。我还没有找到通过这种方式访问解码内容的方法。
import email
data = file('Openworkorders.eml').read()
msg = email.message_from_string(data)
for part in msg.walk():
if part.get_content_type() == 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet':
name = part.get_param('name') or 'MyDoc.doc'
f = open(name, 'wb')
f.write(part.get_payload(None, True))
f.close()
print part.get("content-transfer-encoding")
MyDoc.doc
,应该使用类似于Openworkorders.ods
或Openworkorders.xlsx
的文件。尝试使用 Excel 或 OpenOffice 打开保存的文件。 - stovfleml
=>f.write(part.get_payload(
=>*.xlsx
=> 用Excel打开 == 已验证。这个可行吗? - stovfl