How to Get Macro Info from a DOCX File in Python

Cloudmersive
2 min readMar 17, 2021

--

Enabling macros within Microsoft Word documents can be incredibly helpful to automate repetitive tasks and reduce processing time. However, macros also have a history of being the vehicles for malicious content distribution online. These infected macros could perform a wide array of damage within your computer by running arbitrary commands or deleting files on your hard drive. The following API can be used in Python to retrieve information about the macros in a DOCX file; this will ensure that any enabled macros are risk-free.

To use this API, we first need to install the SDK:

pip install cloudmersive-convert-api-client

Once the client is installed, we are ready to call the get macro function:

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))
input_file = '/path/to/inputfile' # file | Input file to perform the operation on.
try:
# Get macro information from a Word DOCX/DOCM document
api_response = api_instance.edit_document_docx_get_macro_information(input_file)
pprint(api_response)
except ApiException as e:
print("Exception when calling EditDocumentApi->edit_document_docx_get_macro_information: %s\n" % e)

The returned response will give you the information you need to determine if you want to keep the macros enabled or disable them for safety.

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

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

No responses yet