我想将标签导出为pdf文件。我收到错误消息:

'QLabel' object has no attribute 'document'


我设法用textedit做到了这一点(这就是textedit仍然在代码中的原因)。任何我应该怎么做的想法?

谢谢

import sys
from PyQt5 import QtGui
from PyQt5.QtWidgets import QApplication, QMainWindow,  QAction, QTextEdit, QFileDialog,QLabel
from PyQt5.QtPrintSupport import QPrinter
from PyQt5.Qt import QFileInfo


class Window(QMainWindow):
    def __init__(self):
        super().__init__()
        self.title = "PyQt5 export pdf"
        self.top = 200
        self.left = 500
        self.width = 680
        self.height = 480
        self.setWindowTitle(self.title)
        self.setGeometry(self.left, self.top, self.width, self.height)
        self.createEditor()
        self.CreateMenu()
        self.show()

    def CreateMenu(self):
        mainMenu = self.menuBar()
        fileMenu = mainMenu.addMenu('File')
        exportpdfAction = QAction(QIcon("pdf.png"), "Export PDF", self)
        exportpdfAction.triggered.connect(self.printPDF)
        fileMenu.addAction(exportpdfAction)

    def createEditor(self):
        self.label = QLabel("I would like to print this")
        self.textEdit = QTextEdit(self) #I can print that if I want
        self.setCentralWidget(self.label)

    def printPDF(self):
        fn, _ = QFileDialog.getSaveFileName(self, 'Export PDF', None, 'PDF files (.pdf);;All Files()')
        if fn != '':
            if QFileInfo(fn).suffix() == "" : fn += '.pdf'
            printer = QPrinter(QPrinter.HighResolution)
            printer.setOutputFormat(QPrinter.PdfFormat)
            printer.setOutputFileName(fn)
            self.label.document().print_(printer)


App = QApplication(sys.argv)
window = Window()
App.exec()

最佳答案

QTextEditQTextDocument具有print_()方法,但QLabel并非如此,因此您必须将render()方法与QPainter一起使用:

from PyQt5 import QtCore, QtGui, QtWidgets, QtPrintSupport


def print_widget(widget, filename):
    printer = QtPrintSupport.QPrinter(QtPrintSupport.QPrinter.HighResolution)
    printer.setOutputFormat(QtPrintSupport.QPrinter.PdfFormat)
    printer.setOutputFileName(filename)
    painter = QtGui.QPainter(printer)

    # start scale
    xscale = printer.pageRect().width() * 1.0 / widget.width()
    yscale = printer.pageRect().height() * 1.0 / widget.height()
    scale = min(xscale, yscale)
    painter.translate(printer.paperRect().center())
    painter.scale(scale, scale)
    painter.translate(-widget.width() / 2, -widget.height() / 2)
    # end scale

    widget.render(painter)
    painter.end()


class Window(QtWidgets.QMainWindow):
    def __init__(self):
        super().__init__()
        title = "PyQt5 export pdf"
        top, left, width, height = 200, 500, 680, 480
        self.setWindowTitle(title)
        self.setGeometry(left, top, width, height)
        self.createEditor()
        self.CreateMenu()
        self.show()

    def CreateMenu(self):
        mainMenu = self.menuBar()
        fileMenu = mainMenu.addMenu("File")
        exportpdfAction = QtWidgets.QAction(QtGui.QIcon("pdf.png"), "Export PDF", self)
        exportpdfAction.triggered.connect(self.printPDF)
        fileMenu.addAction(exportpdfAction)

    def createEditor(self):
        self.label = QtWidgets.QLabel("I would like to print this")
        self.setCentralWidget(self.label)

    def printPDF(self):
        fn, _ = QtWidgets.QFileDialog.getSaveFileName(
            self, "Export PDF", None, "PDF files (.pdf);;All Files()"
        )
        if fn:
            if QtCore.QFileInfo(fn).suffix() == "":
                fn += ".pdf"

            print_widget(self.label, fn)


if __name__ == "__main__":
    import sys

    App = QtWidgets.QApplication(sys.argv)
    window = Window()
    App.exec()

关于python - 将小部件导出为PDF文件,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57286334/

10-14 19:08