[Python] I have bee trying to create pdf report using Qtablewidget values on template.pdf. pdf...

Discussão em 'Python' iniciado por Stack, Maio 22, 2025.

  1. Stack

    Stack Membro Participativo

    my code seem working but values is shown in saved file.

    def print_pdf(self):
    # path and format date
    desktop= os.path.join(os.path.expanduser("~"), "Desktop")
    date_str= datetime.now().strftime("%d-%m-%Y")
    output_pdf_path= os.path.join(desktop, f"export_{date_str}.pdf")

    # load existinf template
    doc= fitz.open("template.pdf")
    page= doc[0]

    # deefine starting position on pdf
    x,y= 50, 100
    row_height= 20

    # loop through Qtablewidget
    for row in range(self.ui.tableWidgetMainWindow.rowCount()):
    for col in range(self.ui.tableWidgetMainWindow.columnCount()):
    item = self.ui.tableWidgetMainWindow.item(row,col)
    if item:
    text= item.text()

    # compute x,y sapcing for each cell
    page.insert_text((x+col*100, y+row * row_height), text, fontsize=10)

    # save to desktop
    doc.save(output_pdf_path)
    doc.close()
    print(f"Saved to {output_pdf_path}")

    Continue reading...

Compartilhe esta Página