首页 > 解决方案 > 试图使用 Python QT 文本浏览器来显示图片嵌入的 html 电子邮件

问题描述

我正在尝试使用 Python qt textbrower 在 html 中显示 pic 嵌入式电子邮件,但 siplayed 的图片就像这样Html 无法显示嵌入式图片

似乎文本浏览器无法从电子邮件中找到图片,Html 内容和附件文件是使用名为 Zmail 的邮件处理模块中的“get_mail”获得的,附件显示在 txt 文件中 通过打印打印输出时链接到一个驱动器(附件)

我不知道如何处理附件文件,以便可以在 textBrowser 中显示 html 中的嵌入图片

我是这里的新手,任何指导将不胜感激下面是代码

import sys
from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(800, 600)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.textBrowser = QtWidgets.QTextBrowser(self.centralwidget)
        self.textBrowser.setEnabled(True)
        self.textBrowser.setGeometry(QtCore.QRect(210, 60, 550, 300))
        self.textBrowser.setObjectName("textBrowser")
        MainWindow.setCentralWidget(self.centralwidget)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.textBrowser.setHtml(_translate("MainWindow",
                                                "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n"
                                                "<html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">\n"
                                                "p, li { white-space: pre-wrap; }\n"
                                                "</style></head><body style=\" font-family:\'SimSun\'; font-size:9pt; font-weight:400; font-style:normal;\">\n"
                                                "<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\">Text Browser</p></body></html>"))


class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        self.setupUi(self)

        #This html content comes with attachment fetched from the server

        html_content='''<div dir="ltr">This email is for testing\xa0purposes<div><img src="cid:ii_kommhyoz0" alt="image.png" width="283" height="488"><br></div><div>above is an embedded img</div></div>\r\n'''
        self.textBrowser.setText(html_content)

if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    mainWindow = MainWindow()
    mainWindow.show()
    sys.exit(app.exec_())

标签: pythonhtmlimageemail

解决方案


推荐阅读