简体   繁体   English

如何使用 PyQt5 Python 将 QTableWidget 数据转换为 PDF

[英]How to convert QTableWidget data to PDF using PyQt5 Python

I am working on a python project where I have data stored in QTableWidget .我正在开发一个 python 项目,我将数据存储在QTableWidget I have to export this data into excel sheet and PDF .我必须将这些数据导出到excel sheetPDF I have been able to export data to excel sheet using below code.我已经能够使用下面的代码将数据导出到excel sheet But unable to understand how can I convert it to PDF .但无法理解如何将其转换为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)

Above code is working fine and saving data to excel.上面的代码工作正常并将数据保存到excel。

I have looked into other questions with same topic but all of them are in c++.我已经研究了相同主题的其他问题,但所有问题都在 C++ 中。 I am looking for python equivalent.我正在寻找 python 等价物。

Can anyone give me some good suggestion on how to convert data to PDF .谁能给我一些关于如何将数据转换为PDF好建议。 Please help.请帮忙。 Thanks谢谢

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.在这种特殊情况下,解决方案是创建一个显示表格内容的 HTML,并使用 QTextDocument 和 QPrinter 将 HTML 打印为 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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM