本文通过Python3+PyQt5实现《python Qt Gui 快速编程》这本书13章文档打印功能。本文共通过三种方式:
1,使用HTML和QTextDOcument打印文档
2,使用QTextCusor和QTextDocument打印文档
3,使用QPainter打印文档
使用Qpainter打印文档比QTextDocument需要更操心和复杂的计算,但是QPainter确实能够对输出赋予完全控制。
#!/usr/bin/env python3
import math
import sys
import html
from PyQt5.QtPrintSupport import QPrinter,QPrintDialog
from PyQt5.QtCore import (QDate, QRectF, Qt)
from PyQt5.QtWidgets import (QApplication,QDialog,
QHBoxLayout,QPushButton, QTableWidget, QTableWidgetItem,QVBoxLayout)
from PyQt5.QtGui import (QFont,QFontMetrics,QPainter,QTextCharFormat,
QTextCursor, QTextDocument, QTextFormat,
QTextOption, QTextTableFormat,
QPixmap,QTextBlockFormat)
import qrc_resources
from PyQt5.QtPrintSupport import QPrinter,QPrintDialog
from PyQt5.QtCore import (QDate, QRectF, Qt)
from PyQt5.QtWidgets import (QApplication,QDialog,
QHBoxLayout,QPushButton, QTableWidget, QTableWidgetItem,QVBoxLayout)
from PyQt5.QtGui import (QFont,QFontMetrics,QPainter,QTextCharFormat,
QTextCursor, QTextDocument, QTextFormat,
QTextOption, QTextTableFormat,
QPixmap,QTextBlockFormat)
import qrc_resources
DATE_FORMAT = "MMM d, yyyy"
class Statement(object):
def __init__(self, company, contact, address):
self.company = company
self.contact = contact
self.address = address
self.transactions = [] # List of (QDate, float) two-tuples
def balance(self):
return sum([amount for date, amount in self.transactions])
class Form(QDialog):
def __init__(self, parent=None):
super(Form, self).__init__(parent)
self.printer = QPrinter()
self.printer.setPageSize(QPrinter.Letter)
self.generateFakeStatements()
self.table = QTableWidget()
self.populateTable()
cursorButton = QPushButton("Print via Q&Cursor")
htmlButton = QPushButton("Print via &HTML")
painterButton = QPushButton("Print via Q&Painter")
quitButton = QPushButton("&Quit")
buttonLayout = QHBoxLayout()
buttonLayout.addWidget(cursorButton)
buttonLayout.addWidget(htmlButton)
buttonLayout.addWidget(painterButton)
buttonLayout.addStretch()
buttonLayout.addWidget(quitButton)
layout = QVBoxLayout()
layout.addWidget(self.table)
layout.addLayout(buttonLayout)
self.setLayout(layout)
cursorButton.clicked.connect(self.printViaQCursor)
htmlButton.clicked.connect(self.printViaHtml)
painterButton.clicked.connect(self.printViaQPainter)
quitButton.clicked.connect(self.accept)
self.setWindowTitle("Printing")
def generateFakeStatements(self):
self.statements = []
statement = Statement("Consality", "Ms S. Royal",
"234 Rue Saint Hyacinthe, 750201, Paris")
statement.transactions.append((QDate(2007, 8, 11), 2342))
statement.transactions.append((QDate(2007, 9, 10), 2342))
statement.transactions.append((QDate(2007, 10, 9), 2352))
statement.transactions.append((QDate(2007, 10, 17), -1500))
statement.transactions.append((QDate(2007, 11, 12), 2352))
statement.transactions.append((QDate(2007, 12, 10), 2352))
statement.transactions.append((QDate(2007, 12, 20), -7500))
statement.transactions.append((QDate(2007, 12, 20), 250))
statement.transactions.append((QDate(2008, 1, 10), 2362))
self.statements.append(statement)
statement = Statement("Demamitur Plc",