如何使用 PyQt5 Python 将 QTableWidget 数据转换为 PDF [英] How to convert QTableWidget data to PDF using PyQt5 Python

查看:99
本文介绍了如何使用 PyQt5 Python 将 QTableWidget 数据转换为 PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am working on a python project where I have data stored in QTableWidget. I have to export this data into excel sheet and PDF. I have been able to export data to excel sheet using below code. But unable to understand how can I convert it to 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.

Above code is working fine and saving data to excel.

我已经研究了其他具有相同主题的问题,但它们都在C++.我正在寻找 python 等价物.

I have looked into other questions with same topic but all of them are in c++. I am looking for python equivalent.

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

Can anyone give me some good suggestion on how to convert data to PDF. Please help. Thanks

推荐答案

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

When you review an answer you should not only see the code but the solution itself, that is, the logic that is in the background. In this particular case the solution is to create an HTML that shows the table content, and use QTextDocument with QPrinter to print the HTML to PDF.

综上所述,没有必要进行翻译,因为逻辑清晰,从头开始实现就足够了.

Considering the above, it is not necessary to do the translation since it is enough to implement it from scratch since the logic is clear.

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)

这篇关于如何使用 PyQt5 Python 将 QTableWidget 数据转换为 PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆