使用python将eml转换为markdown

2024-06-11 06:50:39 发布

您现在位置:Python中文网/ 问答频道 /正文

我从gmail下载了eml文件,想把邮件内容转换成降价。 我使用的代码如下:

import os
import email
from markdownify import markdownify as md
from email import message_from_file
from email import policy
from email.parser import BytesParser
import glob

    files = glob.glob("file_gmail.eml") # returns list of files
with open(files[0], "rb") as fp:
    msg = BytesParser(policy=policy.default).parse(fp)
    asunto = msg['subject']
    msgmkd = md(msg.get_body(preferencelist=("html")).get_content())
    file = open(asunto + '.md', 'w')
    file.write(msgmkd)
    file.close()

缺点是它留给我CSS和其他一些东西,我无法删除它。正文开头留下的代码如下:

html [if !mso]><meta http-equiv="X-UA-Compatible" content="IE=edge"><!--<![endif] .ExternalClass { width: 100%; background: inherit; background-color: inherit; } .ExternalClass p, .ExternalClass ul, .ExternalClass ol { Margin: 0; } .undoreset div p, .undoreset p { margin-bottom: 20px; } div[class^="aolmail\_divbody"] { overflow: auto; } [owa] #ac-footer { padding: 20px 0px !important; background: inherit; background-color: inherit; }  @media only screen and (max-width: 600px) { /*----------------
...

有更好的方法吗


Tags: 代码fromimportemailpolicymsgfilesmd