【发布时间】:2019-12-08 17:05:11
【问题描述】:
我想在 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()
【问题讨论】:
标签: python pyqt pyqt5 python-3.7 qprinter