通过 Python 在 PDF 文档中添加表格

如何使用 Python 库在 PDF 中添加表格

如何使用 Python for .NET 库在 PDF 文档中添加表格

为了添加表格,我们将使用 Aspose.PDF for .NET API,这是一款功能丰富、功能强大且易于使用的适用于 python-net 平台的文档操作 API。打开 NuGet 软件包管理器,搜索 aspose.pdf 然后安装。您也可以从软件包管理器控制台使用以下命令。

Python Package Manager Console

pip install aspose-pdf

通过 Python 将表格添加到 PDF


你需要 Aspose.PDF for .NET 在你的环境中试用代码。

1。使用 “文档” 实例加载 PDF。 1。通过其索引访问该页面。 1。创建表对象。 1。设置表格设置(例如设置边框)。 1。填充表。 1。将表格添加到页面中。 1。保存该文件。

在 PDF 中添加表格-Python。

    import aspose.pdf as ap

    input_file = DIR_INPUT_TABLE + "AddTable.pdf"
    output_file = DIR_OUTPUT + "document_with_table_out.pdf"
    # Load source PDF document
    doc = ap.Document(input_file)
    # Initializes a new instance of the Table
    table = ap.Table()
    # Set the table border color as LightGray
    table.border = ap.BorderInfo(ap.BorderSide.ALL, 5, ap.Color.from_rgb(apd.Color.light_gray))
    # Set the border for table cells
    table.default_cell_border = ap.BorderInfo(ap.BorderSide.ALL, 5, ap.Color.from_rgb(apd.Color.light_gray))
    # Create a loop to add 10 rows
    for row_count in range(0, 10):
        # Add row to table
        row = table.rows.add()
        # Add table cells
        row.cells.add("Column (" + str(row_count) + ", 1)")
        row.cells.add("Column (" + str(row_count) + ", 2)")
        row.cells.add("Column (" + str(row_count) + ", 3)")
    # Add table object to first page of input document
    doc.pages[1].paragraphs.add(table)
    # Save updated document containing table object
    doc.save(output_file)