【问题标题】:How to convert QTableWidget data to PDF using PyQt5 Python如何使用 PyQt5 Python 将 QTableWidget 数据转换为 PDF
【发布时间】:2020-09-29 04:18:04
【问题描述】:

我正在开发一个 python 项目,我的数据存储在 QTableWidget 中。我必须将这些数据导出到excel sheetPDF。我已经能够使用下面的代码将数据导出到excel sheet。但无法理解如何将其转换为PDF

filename, _ = QFileDialog.getSaveFileName(self, 'Save File', '', ".xls(*.xls)")
wbk = xlwt.Workbook()
sheet = wbk.add_sheet("sheet", cell_overwrite_ok=True)
style = xlwt.XFStyle()
font = xlwt.Font()
font.bold = True
style.font = font
model = self.home_ui.reports_table.model()

for c in range(model.columnCount()):
    text = model.headerData(c, QtCore.Qt.Horizontal)
    first_col = sheet.col(c+1)
    l = len(text)
    first_col.width = (256 * l) + 1000
    sheet.write(0, c + 1, text, style=style)

for r in range(model.rowCount()):
    text = model.headerData(r, QtCore.Qt.Vertical)
    sheet.write(r + 1, 0, text, style=style)

for c in range(model.columnCount()):
    for r in range(model.rowCount()):
        text = model.data(model.index(r, c))
        sheet.write(r + 1, c + 1, text)

wbk.save(filename)

以上代码运行良好,并将数据保存到 excel。

我查看了其他具有相同主题的questions,但它们都使用 C++。我正在寻找 python 等价物。

谁能给我一些关于如何将数据转换为PDF 的好建议。请帮忙。谢谢

【问题讨论】:

    标签: python pdf pyqt5 qtablewidget


    【解决方案1】:

    查看答案时,您不仅应该看到代码,还应该看到解决方案本身,即后台的逻辑。在这种特殊情况下,解决方案是创建一个显示表格内容的 HTML,并使用 QTextDocument 和 QPrinter 将 HTML 打印为 PDF。

    考虑到以上,没有必要做翻译,因为逻辑清晰,从头实现就足够了。

    from PyQt5 import QtCore, QtGui, QtWidgets, QtPrintSupport
    
    app = QtWidgets.QApplication([])
    
    w = QtWidgets.QTableWidget(10, 10)
    for i in range(10):
        for j in range(10):
            it = QtWidgets.QTableWidgetItem("{}-{}".format(i, j))
            w.setItem(i, j, it)
    
    
    filename = "table.pdf"
    model = w.model()
    
    printer = QtPrintSupport.QPrinter(QtPrintSupport.QPrinter.PrinterResolution)
    printer.setOutputFormat(QtPrintSupport.QPrinter.PdfFormat)
    printer.setPaperSize(QtPrintSupport.QPrinter.A4)
    printer.setOrientation(QtPrintSupport.QPrinter.Landscape)
    printer.setOutputFileName(filename)
    
    doc = QtGui.QTextDocument()
    
    html = """<html>
    <head>
    <style>
    table, th, td {
      border: 1px solid black;
      border-collapse: collapse;
    }
    </style>
    </head>"""
    html += "<table><thead>"
    html += "<tr>"
    for c in range(model.columnCount()):
        html += "<th>{}</th>".format(model.headerData(c, QtCore.Qt.Horizontal))
    
    html += "</tr></thead>"
    html += "<tbody>"
    for r in range(model.rowCount()):
        html += "<tr>"
        for c in range(model.columnCount()):
            html += "<td>{}</td>".format(model.index(r, c).data() or "")
        html += "</tr>"
    html += "</tbody></table>"
    doc.setHtml(html)
    doc.setPageSize(QtCore.QSizeF(printer.pageRect().size()))
    doc.print_(printer)
    

    【讨论】:

    • 谢谢。我正在尝试理解您的代码,因为它包含 html 并且我不熟悉 html。是否可以在数据周围绘制边框以使它们看起来像单元格?
    • @SAndrew 1) 如果您不知道某个主题,那么您应该进行调查,因为如果您想要自定义,那么基础知识是不够的。 2) 在我的更新中,我已经添加了简单的边框。
    • 我得到了它的工作html = '''&lt;table border="1" width="100%"&gt;&lt;thead&gt;'''。 Appologies 刚刚看到您的评论
    • pyqt 接受的 CSS 子集在不断发展。 PyQt 5.9.2 不接受语法“border: 1 px solid black”。它将接受边框宽度、边框样式和边框颜色作为单独的属性。它不会识别表格 CSS 中的边框折叠或宽度属性。
    猜你喜欢
    • 1970-01-01
    • 2020-02-16
    • 1970-01-01
    • 2018-06-24
    • 2015-07-13
    • 1970-01-01
    • 2015-10-07
    • 2021-05-30
    • 1970-01-01
    相关资源
    最近更新 更多