Add Table to PDF using Python

Insert table to PDF document programmatically using Aspose.PDF for Python for .NET Library

How to adding Tables in PDF document Using Python for .NET Library

In order to add table, use Aspose.PDF for Python via .NET, a powerful and easy-to-use API. Open PyPI, search for aspose-pdf, and install it. Alternatively, run the command:

Console

pip install aspose-pdf

Add Table to PDF using Python


You need Aspose.PDF for Python via .NET to try the code in your environment.

  1. Load the PDF with an instance of Document.
  2. Access the Page via its index.
  3. Create Table object.
  4. Set table setting (e.g. set the borders).
  5. Populate table.
  6. Add the table to a page.
  7. Save the file.

Add Table in PDF - Python

import aspose.pdf as apdf

from os import path

path_infile = path.join(self.data_dir, infile)
path_outfile = path.join(self.data_dir, outfile)

document = apdf.Document(path_infile)

border_info = apdf.BorderInfo(apdf.BorderSide.ALL, 5, apdf.Color.light_gray)
table = apdf.Table()

table.border = border_info

table.default_cell_border = border_info

for row_count in range(0, 10):
    row = table.rows.add()
    row.cells.add("Column (" + str(row_count) + ", 1)")
    row.cells.add("Column (" + str(row_count) + ", 2)")
    row.cells.add("Column (" + str(row_count) + ", 3)")

document.pages[1].paragraphs.add(table)
document.save(path_outfile)