python - 将 Gmail 转换为 PDF : embedded images in HTML

标签 python gmail-api mime

我正在使用 Gmail API 下载电子邮件。当这些电子邮件是 HTML 时,我尝试使用 Python 的 pdfkit 将它们转换为 PDF。

这在很多情况下都有效,但在某些情况下,html 负载包含图像标签,如 src=“cid:169abdc4ae2c4da871d2” .

这个“cid”标签似乎是指作为多部​​分电子邮件的一部分发送的图像,但这不能被 PDFkit 处理。错误是:

wkhtmltopdf reported an error:
Loading pages (1/6)
Error: Failed to load cid:169abf0d0cdfffb7aff2, with network status code 301 and http status code 0 - Protocol "cid" is unknown

我该如何解决这个问题?有没有办法将我从 gmail 有效负载获得的 HTML 转换为具有适当图片源的标准 HTML?

最佳答案

您可以在 w3lib Package 中使用“remove_tags”方法:
删除所有标签:

import w3lib.html
doc = '<div><p><b>This is a link:</b> <a href="http://www.example.com">example</a></p></div>'
w3lib.html.remove_tags(doc)
'This is a link: example'
删除特定标签:
 w3lib.html.remove_tags(doc, which_ones=('a','b'))
'<div><p>This is a link: example</p></div>'

关于python - 将 Gmail 转换为 PDF : embedded images in HTML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55317467/

相关文章:

python - 使用 Gmail API 从 Gmail 下载附件

c# - 内容类型 header 的标准命名约定

HTML 格式的 Python 电子邮件 mimelib

javascript - Django:如何使用 Javascript 的 window.confirm() 注销用户

python - 我可以用什么代替 python 2.4 中的 next()

Python:Google API - 从消息中获取 mimeTypes

Python:添加一个新的 mimetype

python - 它是一种使用 itertools.cycle() 了解索引的方法吗?

python - ValueError : not enough values to unpack (expected 8, 得到 7) - 忽略不满足此要求的文件 - python

gmail-api - Gmail api 在调用 message.get 时返回 404 错误