我正在打印一组表格,每个表格都应该有自己的页面并且可能很长。基础工作正常,但我没有画出页脚。问题是页脚将被绘制在额外的文档中。
根据文档,我必须将画家设置为设备。该设备是画家,这是正确的,但我如何将画家设置为正确的块?还是这样做是不对的?
目标是使用此文档两次。第一次尝试是打印,第二次是QTextDocument
我可以在其中找到QTextTable
's 并与另一个文档元素一起编译它。
工作示例
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtPrintSupport import *
content = [['section 1', [1,2,3,4]],['section2', [5,6,7,8]]]
app = QApplication(sys.argv)
document = QTextDocument ()
printer = QPrinter()
painter = QPainter(printer)
pageRect = printer.pageRect ()
tableFormat = QTextTableFormat ()
cellBlockFormat = QTextBlockFormat ()
cellCharFormat = QTextCharFormat ()
cellCharFormat.setFont (QFont ("Arial", 10))
for rownr, line in enumerate(content):
cursor = QTextCursor (document)
mainFrame = cursor.currentFrame ()
# header
cursor.setPosition (mainFrame.firstPosition ())
cursor.insertHtml ("This is the table for %s"%line[0])
# table
table = cursor.insertTable (3, 4, tableFormat)
for colnr, col in enumerate(line[1]):
print("col:", col)
cellCursor = table.cellAt (rownr + 1, colnr).firstCursorPosition ()
cellCursor.setBlockFormat (cellBlockFormat)
cellCursor.insertText (str (col))
#footer
painter.begin(printer)
painter.drawText (0, pageRect.bottom(), "I may be the footer")
painter.end()
# section finished
cursor.setPosition (mainFrame.lastPosition ())
tableFormat.setPageBreakPolicy (QTextFormat.PageBreak_AlwaysAfter)
cursor.insertBlock (cellBlockFormat, cellCharFormat)
document.print_(printer)