将 PyQt 中的 QTreeWidge 转换为 ReportLab 中的表

发布于 2024-12-26 18:25:41 字数 226 浏览 3 评论 0原文

由于报表在 ReportLab 中看起来几乎过于复杂,我试图确定一种方法来简单地添加(如果可能的话,最好通过 Paragraph 类)两个单独的文本,一个位于页面的左侧,另一个位于页面的右侧。尽管我可以上网,但我找不到任何关于如何实现这一目标的解释。那么如果这是可能的,你会怎么做呢?

最后,我想要实现的是将 PyQT 中的 QTreeWidget 中的数据转换为具有相似外观和感觉的 PDF。

提前致谢!

As tables seem almost overly complicated in ReportLab, I'm trying to determine a means to simply add (preferably through the Paragraph class, if possible) two separate texts, one on the left side of the page and the other on the right. As much as I could internet I could find no seeming explanation of how to accomplish this. So if this is possible, how do you do it?

At the end what I'm trying to pull off is converting data from a QTreeWidget in PyQT into a PDF with similar look and feel.

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

开始看清了 2025-01-02 18:25:41

看来实现这一点最好通过表格来完成。因此,尽管它很复杂,但学习表数据嵌套列表的结构是正确的方法。转换 QTreeWidget 数据的关键在于下面的代码行,因为您必须在处理表数据时动态附加数据和单元格样式。

假设 QTreeWidget 的组合只是具有两列文本(0 和 1)的项目,则以下内容有效。

from reportlab.lib.units import inch
from reportlab.lib.pagesizes import letter
from reportlab.platypus import SimpleDocTemplate, Table, TableStyle

pdf = SimpleDocTemplate("TreeWidgetPDF.pdf", pagesize = letter)
data = []
tStyle = []

for x in QTreeWidgetData.findItems("*", Qt.MatchWildcard, 0):
    project = str(x.text(0))
    data.append([project, x.text(1)])
    tStyle.append(('BACKGROUND', (0, cell), (1, cell), 'YELLOW'))
    tStyle.append(('FONTSIZE', (0, cell), (1, cell), 12))
    cell+=1

    for y in range(x.childCount()):
        data.append([str(x.child(y).text(0)), str(x.child(y).text(1))])
        tStyle.append(('ALIGN', (1, cell), (1, cell), 'RIGHT'))
        tStyle.append(('LEFTPADDING', (0, cell), (0, cell), 15))
        cell+=1

        for z in range(x.child(y).childCount()):
            data.append([x.child(y).child(z).text(0), x.child(y).child(z).text(1)])
            tStyle.append(('ALIGN', (1, cell), (1, cell), 'RIGHT'))
            tStyle.append(('LEFTPADDING', (0, cell), (0, cell), 30))
            cell+=1

        # And so on and so forth. You could probably iterate through this in a 
        # While loop so you don't have to manually nest your for statements.

parts = []
styledTable = Table(data, [6 * inch, 1 * inch, 0])
styledTable.setStyle(TableStyle(tStyle))
parts.append(table_with_style)
pdf.build(parts)

It would appear that accomplishing this is best done through Tables. So though it be convoluted, learning the structure of table data nested lists was the way to go. The key for converting QTreeWidget data goes along the lines of the codes below, in that you have to dynamically append both data and cell styling as you work your way through the table data.

Assuming the QTreeWidget's composition is simply items with two columns of text (0, and 1), the below works.

from reportlab.lib.units import inch
from reportlab.lib.pagesizes import letter
from reportlab.platypus import SimpleDocTemplate, Table, TableStyle

pdf = SimpleDocTemplate("TreeWidgetPDF.pdf", pagesize = letter)
data = []
tStyle = []

for x in QTreeWidgetData.findItems("*", Qt.MatchWildcard, 0):
    project = str(x.text(0))
    data.append([project, x.text(1)])
    tStyle.append(('BACKGROUND', (0, cell), (1, cell), 'YELLOW'))
    tStyle.append(('FONTSIZE', (0, cell), (1, cell), 12))
    cell+=1

    for y in range(x.childCount()):
        data.append([str(x.child(y).text(0)), str(x.child(y).text(1))])
        tStyle.append(('ALIGN', (1, cell), (1, cell), 'RIGHT'))
        tStyle.append(('LEFTPADDING', (0, cell), (0, cell), 15))
        cell+=1

        for z in range(x.child(y).childCount()):
            data.append([x.child(y).child(z).text(0), x.child(y).child(z).text(1)])
            tStyle.append(('ALIGN', (1, cell), (1, cell), 'RIGHT'))
            tStyle.append(('LEFTPADDING', (0, cell), (0, cell), 30))
            cell+=1

        # And so on and so forth. You could probably iterate through this in a 
        # While loop so you don't have to manually nest your for statements.

parts = []
styledTable = Table(data, [6 * inch, 1 * inch, 0])
styledTable.setStyle(TableStyle(tStyle))
parts.append(table_with_style)
pdf.build(parts)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文