How to merge PDFs using Python
Table of contents
Merge multiple PDF files using our Python PDF merging API. Create a free account, get API credentials, and implement merging using the requests library. Combine with 30+ other API tools for watermarking, OCR, and document manipulation.
In this post, you’ll learn how to combine multiple PDF files using our Python PDF merging API. With our API, you receive 200 credits with the free plan. Different operations on a document consume different amounts of credits, so the number of PDF documents you can generate may vary. You’ll just need to create a free account(opens in a new tab) to get access to your API key.
Nutrient DWS Processor API
Document merging is just one of our 30+ PDF API tools. You can combine our merging tool with other tools to create complex document processing workflows:
- Convert MS Office files and images into PDFs before merging
- OCR several documents before merging
- Merge, watermark, and flatten PDFs
Once you create your account, you’ll be able to access all our PDF API tools.
Getting started
To get started, you’ll need to:
- Create a free account to access your live API key.
- Install Python on your system. You can download Python here(opens in a new tab).
Step 1 — Creating a free account on Nutrient
Go to our website(opens in a new tab) and create your free account. You’ll see the page below.

Once you’ve created your account, you’ll be welcomed by the page below.

As you can see in the bottom-left corner, you’ll start with 200 credits to process.
Copy the Live API key, because you’ll need this for the merge PDF API.
Step 2 — Setting up folders and files
Now, create a folder called merge_pdf and open it in a code editor. For this tutorial, you’ll use VS Code as your primary code editor. Next, create two folders inside merge_pdf and name them input_documents and processed_documents.
Then, in the root folder, merge_pdf, create a file called processor.py. This is the file where you’ll keep your code.
Now your folder structure will look like this.

Step 3 — Writing the code
Assuming you already have Python installed, open the processor.py file in your code editor. Paste the code below into your file:
import requestsimport json
response = requests.request( 'POST', 'https://api.nutrient.io/build', headers = { 'Authorization': 'Bearer your_api_key_here' }, files = { 'first_half': open('input_documents/first_half.pdf', 'rb'), 'second_half': open('input_documents/second_half.pdf', 'rb') }, data = { 'instructions': json.dumps({ 'parts': [ { 'file': 'first_half' }, { 'file': 'second_half' } ] }) }, stream = True)
if response.ok: with open('processed_documents/result.pdf', 'wb') as fd: for chunk in response.iter_content(chunk_size=8096): fd.write(chunk)else: print(response.text) exit()In the first two lines of your code, you’re importing the two dependencies:
requests— You’ll use this module to make API calls.json— This will convert a dictionary to JSON.
After that, prepare the JSON that’ll be sent as a payload to the merge PDF API. You’re also passing the input files from the input document folder in the JSON. This calls the API and stores the response in the processed_documents folder of your Python application.
You can find this code on our API website. You’ll also find the code written in other popular languages, like PHP, C#, Java, JavaScript, and Shell.
Now, copy and paste the PDF files you want to process in the input_documents folder. For this tutorial, you’ll use these two PDF files: first_half.pdf and second_half.pdf.
Your updated folder structure will look like this.

Once done, paste your API key into the code.
Step 4 — Output
To execute the Python code, use the command below:
python3 processor.pyOnce the code has been executed, you’ll see a new PDF file under the processed_documents folder.

If you open the result.pdf file, you’ll see that the number of pages is equal to the sum of both input file pages.
Additional resources
Explore more ways to work with Nutrient API:
- Postman collection — Test API endpoints directly in Postman
- Zapier integration — Automate document workflows without code
- MCP Server — PDF automation for LLM applications
- Python client — Official Python library
Conclusion
That’s it! In this post, you learned how to merge files for your Python application into a single PDF using our merge PDF API.
If you have a more complex use case, you can use our other tools to add watermarks, perform OCR, and edit (split, flatten, delete, duplicate) documents — and you can even combine these tools. To get started with a free trial, sign up here(opens in a new tab).
FAQ
Nutrient DWS Processor API offers 30+ PDF operations, including splitting, watermarking, OCR, flattening, and converting Office documents to PDF. You can combine these operations in a single workflow. For example, merge multiple PDFs, watermark the result, then flatten it to prevent editing — all through the same API.
Yes! Use our Postman collection to test all API endpoints directly in Postman. Import the collection, add your API key, and experiment with different operations and parameters. This helps you understand the API before integrating it into your Python application. You can also test using tools like cURL or HTTPie in your terminal.
Use our Zapier integration to automate PDF processing without writing code. Connect Nutrient DWS Processor API with 5,000+ apps like Google Drive, Dropbox, Gmail, and Slack. For example, automatically merge PDFs when they’re uploaded to a specific Google Drive folder, or combine email attachments and save the merged result.
Nutrient DWS API doesn’t store any input or output documents on its infrastructure. Files are processed in memory and deleted immediately after your request completes. All communication uses HTTPS encryption. For enhanced security requirements, Nutrient also offers self-hosted deployment options where documents never leave your infrastructure.
Yes. Add multiple entries to the files dictionary and reference them in the parts array. For example, to merge three PDFs, include 'third': open('input_documents/third.pdf', 'rb') in your files and add {'file': 'third'} to your parts array. The API processes files in the order specified in the parts array, allowing you to control the final document structure.