Adicionando tabelas em um documento PDF via Python

Como adicionar tabela em PDF usando Python Biblioteca

Como adicionar tabelas em um documento PDF usando a biblioteca Python for .NET

Para adicionar tabela, usaremos a API Aspose.PDF for .NET, que é uma API de manipulação de documentos rica em recursos, poderosa e fácil de usar para a plataforma python-net. Abra o gerenciador de pacotes NuGet, procure por Aspose.pdf e instale. Você também pode usar o seguinte comando no Console do Gerenciador de Pacotes.

Python Package Manager Console

pip install aspose-pdf

Adicionar tabela ao PDF via Python


Você precisa Aspose.PDF for .NET testar o código em seu ambiente.

  1. Carregue o PDF com uma instância do Document.
  2. Acesse a página por meio de seu índice.
  3. Objeto Create Table.
  4. Definir a configuração da tabela (por exemplo, definir as bordas).
  5. Preencher tabela.
  6. Adicione a tabela a uma página.
  7. Salve o arquivo.

Adicionar tabela em 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)