En

KNOWLEDGEBASE

Tables in Python | Aspose

In this article we will work with tables in Python, we will show you the easiest way to create a table and manipulate the data inside the table. Using our API, you can manipulate tables and export them to different formats, such as Word, Excel, PDF, etc.
We’ll introduce Aspose.Words for Python via .NET, and focus on creating and inserting tables using this API.

Tables in Python

Aspose.Words for Python via .NET is a class library designed to read and manipulate documents of numerous types like Microsoft Word (DOCX, DOC, ODT), Web (Markdown, HTML), PDF, and others. With this Python API, you can create, edit, render, and convert Word files to multiple formats, generate reports, and visualize your data without the need for external software.
More than 100 Python classes for handling document processing and data formatting operations are at your disposal.

Working with Tables in Python

Tables allow organizing large amounts of information and displaying it in a grid-like structure with rows and columns. They are very useful when displaying tabbed data as they allow much better control over the design of the content.
Tables are structured sets of data composed of rows and columns. They are fully supported in our library, and you can easily edit, change, add and remove tables.
Below we will show you how to create a new table using Python, and in our documentation, you can see how to apply formatting, work with TableStyle, work with columns and rows, and join and split tables.

Creating tables in Python

Our library offers different methods for creating new tables within a document, and in this article, we will see how to use some of these methods.
The newly created table has values similar to the default values of Microsoft Word.

Table property Python

You can insert a table using DocumentBuilder class, and the following method to build the table:
DocumentBuilder.start_table
DocumentBuilder.insert_cell
DocumentBuilder.end_row
DocumentBuilder.end_table
DocumentBuilder.writeln

Build table methods Python

Create a table in Python using these 7 steps:

  1. Start the table with DocumentBuilder.start_table.
  2. Use DocumentBuilder.insert_cell to insert a cell inside the table, and use the DocumentBuilder.cell_format to specify cell formatting.
  3. Use the DocumentBuilder to insert cell contents.
  4. Just repeat steps 2 and 3 until you complete the row.
  5. You can call DocumentBuilder.end_row method to end the row and use DocumentBuilder.row_format to do row formatting.
  6. After that, just repeat steps 2 through 5 until you complete the table.
  7. Use DocumentBuilder.end_table method to finish the table.

More details about DocumentBuilder class and methods for table creation are described in our documentation.

In the example below you can see how to create a simple table with default formatting using DocumentBuilder.

# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET
doc = aw.Document()
builder = aw.DocumentBuilder(doc)
    
# Start building the table.
builder.start_table()
builder.insert_cell()
builder.write("Row 1, Cell 1 Content.")
    
# Build the second cell.
builder.insert_cell()
builder.write("Row 1, Cell 2 Content.")
    
# Call the following method to end the row and start a new row.
builder.end_row()

# Build the first cell of the second row.
builder.insert_cell()
builder.write("Row 2, Cell 1 Content")

# Build the second cell.
builder.insert_cell()
builder.write("Row 2, Cell 2 Content.")
builder.end_row()

# Signal that we have finished building the table.
builder.end_table()

doc.save(docs_base.artifacts_dir + "WorkingWithTables.create_simple_table.docx")

Create a formatted table using DocumentBuilder with the code example shown below.

# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET
doc = aw.Document()
builder = aw.DocumentBuilder(doc)

table = builder.start_table()
builder.insert_cell()

# Table wide formatting must be applied after at least one row is present in the table.
table.left_indent = 20.0

# Set height and define the height rule for the header row.
builder.row_format.height = 40.0
builder.row_format.height_rule = aw.HeightRule.AT_LEAST

builder.cell_format.shading.background_pattern_color = drawing.Color.from_argb(198, 217, 241)
builder.paragraph_format.alignment = aw.ParagraphAlignment.CENTER
builder.font.size = 16
builder.font.name = "Arial"
builder.font.bold = True

builder.cell_format.width = 100.0
builder.write("Header Row,\n Cell 1")

# We don't need to specify this cell's width because it's inherited from the previous cell.
builder.insert_cell()
builder.write("Header Row,\n Cell 2")

builder.insert_cell()
builder.cell_format.width = 200.0
builder.write("Header Row,\n Cell 3")
builder.end_row()

builder.cell_format.shading.background_pattern_color = drawing.Color.white
builder.cell_format.width = 100.0
builder.cell_format.vertical_alignment = aw.tables.CellVerticalAlignment.CENTER

# Reset height and define a different height rule for table body.
builder.row_format.height = 30.0
builder.row_format.height_rule = aw.HeightRule.AUTO
builder.insert_cell()
    
# Reset font formatting.
builder.font.size = 12
builder.font.bold = False

builder.write("Row 1, Cell 1 Content")
builder.insert_cell()
builder.write("Row 1, Cell 2 Content")

builder.insert_cell()
builder.cell_format.width = 200.0
builder.write("Row 1, Cell 3 Content")
builder.end_row()

builder.insert_cell()
builder.cell_format.width = 100.0
builder.write("Row 2, Cell 1 Content")

builder.insert_cell()
builder.write("Row 2, Cell 2 Content")

builder.insert_cell()
builder.cell_format.width = 200.0
builder.write("Row 2, Cell 3 Content.")
builder.end_row()
builder.end_table()

doc.save(docs_base.artifacts_dir + "WorkingWithTables.formatted_table.docx")

Inserting an existing table

If you already have a table in your document and want to add a copy of this table in order to make some changes to it, the easiest way to duplicate a table is using the Table.clone method to keep all the formatting.
In the example shown below, you can see how to insert a table, and if you need the template file of this example, you can download it from here.

# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET
doc = aw.Document(docs_base.my_dir + "Tables.docx")

table = doc.get_child(aw.NodeType.TABLE, 0, True).as_table()

# Clone the table and insert it into the document after the original.
tableClone = table.clone(True).as_table()
table.parent_node.insert_after(tableClone, table)

# Insert an empty paragraph between the two tables,
# or else they will be combined into one upon saving this has to do with document validation.
table.parent_node.insert_after(aw.Paragraph(doc), table)
    
doc.save(docs_base.artifacts_dir + "WorkingWithTables.clone_complete_table.docx")

If you have questions about tables, Aspose.Words for Python via .NET products and need help from our Paid Support developers to implement our API in your project, feel free to contact us.