Tables in Word Documents with Python
Tables in Word Documents with Python
Add or delete a table, add a row or a column, extract data from a table, - all this and much more you can do with Word documents in Python with Cloud API.
If you have worked in Microsoft Word, then you understand the main concept of working with tables to create text and graphics or showing data in a tabular
style (similar to a spreadsheet).
In a standard document page text, graphics and other content appear in a free-flowing way. The text and images wrap, and one item appears after the next. There is
no way to control where items appear in the document, and this is where tables become useful. You can use a table to structure content, place text or images
in one cell and align it accordingly. Furthermore, tables can be nested inside other tables up to any depth with Python API.
Note: As a prerequisite, you need Python and Words Cloud Python SDK installed on your computer.
Creating and Formatting Tables with Word
A table is made up of rows and columns. The intersection of a row and column is called a cell.
You can use Cloud API in Python to create a table, format it, apply a style, modify a table in code, insert data into rows and cells.
A table is imported as a Table node from any document loaded into the Cloud Storage. Aspose.Words.Tables namespace contains classes that represent tables,
rows, cells and their formatting.
Table manipulation in Python
You can perform the following tasks with tables in DOCX, DOC, RTF and document file formats using Python:
- Add or delete a table.
- Resize a table.
- Change columns width in a table.
- Change rows height in a table.
- Convert a table to text.
- Text wrap to surround a picture or a diagram with text.
- Add rows or columns to a Table.
- Delete rows or columns in a Table.
- Change properties of a table, a row or a cell.
Extract data from tables using Python
Python API allows you to extract text from tables. You can call API methods to get a row or to get a cell from a table.
Working with borders and margins
You can set border style, margins and cell padding in tables with Python API. You can use border property to set a border for a table. You can apply borders
to a table, to each cell or to a single cell.
Cell margin represents the number of blank spaces between the content of a cell and the border of a cell. Usually, all cells have the same margin in the
table.
Working with margins, give you the right way to control spacing in a Word table.
Table properties with Python API
A table has the following properties:
AutoFit
Resize a table, a column or a row using AutoFit option.
Turn on AutoFit to adjust tables or columns to fit the size of your content automatically. Otherwise, turn it off for Fixed Column Width option.
Alignment
You can align a table between page margins either right, left, or center with Python API.
Merge a table
Python API allows you to merge two or more tables into one larger table. After merging tables, you can also merge specific cells within the table.
Split a table
It allows you to split a table into two or more tables. It creates smaller tables or adds text between two tables.
How to work with tables in a DOCX document in Python
- Go to the Aspose Cloud Dashboard.
- Create a new Account to access all applications and services or Sign In to your account.
- Click on Applications in the left menu to get Client Id and Client Secret.
- Check API Reference page for the description of APIs parameters.
- Install Python SDK.
- Check Developer Guide to work with tables in a DOCX file in Python.
import asposewordscloud
from asposewordscloud.models.requests import InsertTableRequest, GetTableWithoutNodePathRequest
from asposewordscloud import TableInsert
app_sid = '####-####-####-####-####'
app_key = '##################'
words_api = WordsApi(app_sid, app_key)
# Add a table
table = TableInsert(columns_count=3, rows_count=5)
request = InsertTableRequest('sample.docx', table, node_path='sections/0')
words_api.insert_table(request)
# Extract data from table
request = GetTableWithoutNodePathRequest('sample.docx', index=0)
data = words_api.get_table_without_node_path(request)
print(data)
# Edit a cell at row 2, column 3
paragraph = ParagraphInsert(text='Lorem Ipsum')
request = InsertParagraphRequest('sample.docx', paragraph, node_path='sections/0/tables/0/rows/1/cells/2')
words_api.insert_paragraph(request)
Support and Learning Resources
- Learning Resources
- Documentation
- Source Code
- API References
- Tutorial Videos
- Product Support
- Free Support
- Paid Support
- Blog
- Why Aspose.Words Cloud for Python?
- Customers List
- Security