PyQt5将Textedit保存到图像中

时间:2018-01-04 14:20:23

标签: python pyqt pyqt5

我想将Qtexedit的文字保存到最适合PNG的图像中。

我在Rich text to image in Qt

的帖子Dmitry Sazonov中读到了

可以使用QTextEdit::document QTextDocument::drawContents

完成

但是我没有找到任何例子。

1 个答案:

答案 0 :(得分:1)

您必须做的第一件事是创建一个具有适当大小的QPixmap,用一些背景颜色填充它,然后创建一个QPainter,这将负责绘制{{1} },我们使用QPixmap绘制,我们使用drawContents()完成绘画,最后我们使用end() save()函数保存它。

示例:

QPixmap

<强> GUI

enter image description here

<强> PNG

enter image description here

加号:

如果要反转每个像素的颜色,使用from PyQt5.QtCore import * from PyQt5.QtGui import * from PyQt5.QtWidgets import * html = """<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN" "http://www.w3.org/TR/REC-html40/strict.dtd"> <html><head><meta name="qrichtext" content="1" /><style type="text/css"> p, li { white-space: pre-wrap; } </style></head><body style=" font-family:'Cantarell'; font-size:11pt; font-weight:400; font-style:normal;"> <p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span style=" font-family:'Open Sans,Arial,sans-serif'; font-size:14px; font-style:italic; color:#000000; background-color:#ffffff;">Lorem ipsum</span><span style=" font-family:'Open Sans,Arial,sans-serif'; font-size:14px; color:#000000; background-color:#ffffff;"> dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. </span><span style=" font-family:'Open Sans,Arial,sans-serif'; font-size:14px; font-weight:600; color:#000000; background-color:#ffffff;">Duis aute irure dolor</span><span style=" font-family:'Open Sans,Arial,sans-serif'; font-size:14px; color:#000000; background-color:#ffffff;"> in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, </span><span style=" font-family:'Open Sans,Arial,sans-serif'; font-size:14px; text-decoration: underline; color:#000000; background-color:#ffffff;">sunt in culpa qui officia deserunt mollit anim id est laborum.</span></p> <p style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; font-family:'Open Sans,Arial,sans-serif'; font-size:14px; text-decoration: underline; color:#000000;"><br /></p> <p align="center" style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span style=" font-family:'Open Sans,Arial,sans-serif'; font-size:14px; color:#000000; background-color:#ffffff;">Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur?</span></p></body></html>""" def save(doc): pixmap = QPixmap(doc.idealWidth(), doc.size().height()) pixmap.fill(Qt.white) painter = QPainter(pixmap) doc.drawContents(painter, QRectF(0, 0, doc.idealWidth(), doc.size().height())) painter.end() pixmap.save("output.png", "PNG") if __name__ == '__main__': import sys app = QApplication(sys.argv) w = QWidget() lay = QVBoxLayout(w) btn = QPushButton("Save") te = QTextEdit() lay.addWidget(btn) lay.addWidget(te) te.setHtml(html) w.show() btn.clicked.connect(lambda checked, doc=te.document(): save(doc)) sys.exit(app.exec_()) 更合适:

QImage