How to Change PDF Metadata in Python

Cloudmersive
2 min readMar 21, 2024

--

Using just a few lines of Python code, we can easily change a PDF’s metadata to our liking.

We’ll be calling a free API with ready-to-run, complementary Python code examples. We can structure our PDF metadata input request following the below example:

pip install cloudmersive-convert-api-client

First things first, of course — let’s start by installing the client SDK via pip install:

pip install cloudmersive-convert-api-client

Next, let’s turn our attention to authorization. We’ll need a free Cloudmersive API key to make our API calls (this allows a limit of 800 API calls per month with no further commitments).

With our API key ready, let’s add the imports and call the 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.EditPdfApi(cloudmersive_convert_api_client.ApiClient(configuration))
request = cloudmersive_convert_api_client.SetPdfMetadataRequest() # SetPdfMetadataRequest |

try:
# Sets PDF document metadata
api_response = api_instance.edit_pdf_set_metadata(request)
pprint(api_response)
except ApiException as e:
print("Exception when calling EditPdfApi->edit_pdf_set_metadata: %s\n" % e)

We can pass our PDF metadata input request along with our PDF file path and we’ll return a new PDF with metadata values set to our liking. All done!

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

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

No responses yet