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

Discussão em 'Python' iniciado por Stack, Abril 29, 2025.

  1. Stack

    Stack Membro Participativo

    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)


    Above code is working fine and saving data to excel.

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

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

    Continue reading...

Compartilhe esta Página