Add a New Table to a Word DOCX File in Ruby

Cloudmersive
2 min readAug 23, 2021

Data tables are not the easiest feature to add to an online Word document. If you need to include a table if your DOCX file, one way to simplify the process is by using an API to perform the action for you. The following API can be used in conjunction with our Begin Editing and Finish Editing APIs to insert a new table into a Word document, ensuring a smooth editing experience.

Our first step is to add the Ruby client to the Gemfile:

gem 'cloudmersive-convert-api-client', '~> 2.1.6'

Next, we will need to fill out the applicable information in the input request:

{
"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"
}

Now to call the function, we will add the input request and our API key into the below code:

# load the gem
require 'cloudmersive-convert-api-client'
# setup authorization
CloudmersiveConvertApiClient.configure do |config|
# Configure API key authorization: Apikey
config.api_key['Apikey'] = 'YOUR API KEY'
# Uncomment the following line to set a prefix for the API key, e.g. 'Bearer' (defaults to nil)
#config.api_key_prefix['Apikey'] = 'Bearer'
end
api_instance = CloudmersiveConvertApiClient::EditDocumentApi.newreq_config = CloudmersiveConvertApiClient::InsertDocxTablesRequest.new # InsertDocxTablesRequest | Document input requestbegin
#Insert a new table into a Word DOCX document
result = api_instance.edit_document_docx_insert_table(req_config)
p result
rescue CloudmersiveConvertApiClient::ApiError => e
puts "Exception when calling EditDocumentApi->edit_document_docx_insert_table: #{e}"
end

If you have no other editing operations to perform on the document, you can close it out and download the updated file with the aforementioned Finish Editing API.

--

--

Cloudmersive

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