How to Insert a Table in DOCX Using Python

Cloudmersive
2 min readJan 26, 2021

--

Need to add a new table to your online Word document? We’re here to make inserting your rows and columns of information easier. This helpful process is one of several editing tools you can utilize after opening up editing with our Begin Editing API.

In order to ensure an accurate output, be sure to input the request configuration:

{
"InputFileBytes": "string",
"InputFileUrl": "string",
"TableToInsert": {
"TableID": "string",
"Path": "string",
"Width": "string",
"WidthType": "string",
"TableRows": [
{
"RowIndex": 0,
"Path": "string",
"RowCells": [
{
"CellIndex": 0,
"Path": "string",
"Paragraphs": [
{
"ParagraphIndex": 0,
"Path": "string",
"ContentRuns": [
{
"RunIndex": 0,
"Path": "string",
"TextItems": [
{
"TextIndex": 0,
"Path": "string",
"TextContent": "string"
}
],
"Bold": true,
"Italic": true,
"Underline": "string",
"FontFamily": "string",
"FontSize": "string"
}
],
"StyleID": "string"
}
],
"CellShadingColor": "string",
"CellShadingFill": "string",
"CellShadingPattern": "string",
"CellWidthMode": "string",
"CellWidth": "string"
}
]
}
],
"TopBorderType": "string",
"TopBorderSize": 0,
"TopBorderSpace": 0,
"TopBorderColor": "string",
"BottomBorderType": "string",
"BottomBorderSize": 0,
"BottomBorderSpace": 0,
"BottomBorderColor": "string",
"LeftBorderType": "string",
"LeftBorderSize": 0,
"LeftBorderSpace": 0,
"LeftBorderColor": "string",
"RightBorderType": "string",
"RightBorderSize": 0,
"RightBorderSpace": 0,
"RightBorderColor": "string",
"CellHorizontalBorderType": "string",
"CellHorizontalBorderSize": 0,
"CellHorizontalBorderSpace": 0,
"CellHorizontalBorderColor": "string",
"CellVerticalBorderType": "string",
"CellVerticalBorderSize": 0,
"CellVerticalBorderSpace": 0,
"CellVerticalBorderColor": "string",
"StartBorderType": "string",
"StartBorderSize": 0,
"StartBorderSpace": 0,
"StartBorderColor": "string",
"EndBorderType": "string",
"EndBorderSize": 0,
"EndBorderSpace": 0,
"EndBorderColor": "string",
"TableIndentationMode": "string",
"TableIndentationWidth": 0
},
"InsertPlacement": "string",
"InsertPath": "string"
}

Next, we can install the SDK by running this command:

pip install cloudmersive-convert-api-client

Once the installation is complete, we’re ready to call the insert table function using the following code:

from __future__ import print_function
import time
import cloudmersive_convert_api_client
from cloudmersive_convert_api_client.rest import ApiException
from pprint import pprint
# Configure API key authorization: Apikey
configuration = cloudmersive_convert_api_client.Configuration()
configuration.api_key['Apikey'] = 'YOUR_API_KEY'
# create an instance of the API class
api_instance = cloudmersive_convert_api_client.EditDocumentApi(cloudmersive_convert_api_client.ApiClient(configuration))
req_config = cloudmersive_convert_api_client.InsertDocxTablesRequest() # InsertDocxTablesRequest | Document input request
try:
# Insert a new table into a Word DOCX document
api_response = api_instance.edit_document_docx_insert_table(req_config)
pprint(api_response)
except ApiException as e:
print("Exception when calling EditDocumentApi->edit_document_docx_insert_table: %s\n" % e)

And that’s all, folks; this will nestle the new table into your document, as requested. If you have no other editing functions to complete, be sure to run our Finish Editing API to close the operation and download your updated document.

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.

No responses yet