How to get all tables in a Word DOCX document in Python

Setting up compatibility for DOCX files in Python can be such a bore. There’s really no reason to grind through the process for hours when I can show you how to use an API that will do it in your stead. Today, we are focusing on using it for extracting all tables from a document.

Take the first step with pip install for our API client:

pip install cloudmersive-convert-api-client

Our function, edit_document_docx_get_tables, will get the job done for us. We just need to call it with this little snippet of code here:

from __future__ import print_functionimport timeimport cloudmersive_convert_api_clientfrom cloudmersive_convert_api_client.rest import ApiExceptionfrom pprint import pprint# Configure API key authorization: Apikeyconfiguration = cloudmersive_convert_api_client.Configuration()configuration.api_key['Apikey'] = 'YOUR_API_KEY'# Uncomment below to setup prefix (e.g. Bearer) for API key, if needed# configuration.api_key_prefix['Apikey'] = 'Bearer'# create an instance of the API classapi_instance = cloudmersive_convert_api_client.EditDocumentApi(cloudmersive_convert_api_client.ApiClient(configuration))req_config = cloudmersive_convert_api_client.GetDocxTablesRequest() # GetDocxTablesRequest | Document input requesttry:# Get all tables in Word DOCX documentapi_response = api_instance.edit_document_docx_get_tables(req_config)pprint(api_response)except ApiException as e:print("Exception when calling EditDocumentApi->edit_document_docx_get_tables: %s\n" % e)

And before you know it, we have everything set up. The rest is pretty self-explanatory. Check out the documentation for a whole range of other functions related to productivity, with support for DOCX, XLSX, and other popular formats.

Image for post
Image for post
This solution is so easy, you could set it up with a drawing tablet!

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store