Skip to content

Instantly share code, notes, and snippets.

@aspose-com-gists
Last active November 23, 2021 05:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aspose-com-gists/baa8cef19a815ab614b1ac489791b7f1 to your computer and use it in GitHub Desktop.
Save aspose-com-gists/baa8cef19a815ab614b1ac489791b7f1 to your computer and use it in GitHub Desktop.
Create Tables in Word Documents in Python
import aspose.words as aw
# Load the Word document.
doc = aw.Document("table_formatted.docx")
# Get reference of the desired table.
table = doc.get_child(aw.NodeType.TABLE, 0, True).as_table()
# Clone the table and insert it into the document after the original table.
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.
table.parent_node.insert_after(aw.Paragraph(doc), table)
# Save the document.
doc.save("table_clone.docx")
import aspose.words as aw
# Create a new Word document.
doc = aw.Document()
builder = aw.DocumentBuilder(doc)
# Insert cell.
cell = builder.insert_cell()
builder.writeln("Outer Table Cell 1")
builder.insert_cell()
builder.writeln("Outer Table Cell 2")
# This call is important to create a nested table within the first table.
# Without this call, the cells inserted below will be appended to the outer table.
builder.end_table()
# Move to the first cell of the outer table.
builder.move_to(cell.first_paragraph)
# Build the inner table.
builder.insert_cell()
builder.writeln("Inner Table Cell 1")
builder.insert_cell()
builder.writeln("Inner Table Cell 2")
builder.end_table()
# Save the document.
doc.save("table_nested.docx")
import aspose.words as aw
# Create a new Word document.
doc = aw.Document()
# Create document builder.
builder = aw.DocumentBuilder(doc)
# Start the table.
table = builder.start_table()
# Insert cell.
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
# Set alignment and font settings.
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.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()
# End table.
builder.end_table()
# Save the document.
doc.save("table_formatted.docx")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment