---
title: "How to convert DOCX to PDF using Python"
canonical_url: "https://www.nutrient.io/blog/how-to-convert-docx-to-pdf-using-python/"
md_url: "https://www.nutrient.io/blog/how-to-convert-docx-to-pdf-using-python.md"
last_updated: "2026-07-27T16:03:24.228Z"
description: "Convert DOCX to PDF programmatically using Python and Nutrient DWS Processor API. Step-by-step tutorial with code examples and free API credits."
---

**TL;DR**

Convert DOCX to PDF using Nutrient’s Python API. Create a free account, get API credentials, and implement conversion using the requests library. Combine with 30+ API tools for merging, watermarking, and page manipulation.

Convert DOCX files to PDFs in Python using Nutrient’s [DOCX-to-PDF Python API](https://www.nutrient.io/api/docx-to-pdf-api/). The free plan includes 50 credits. Different operations consume different amounts of credits. Create a [free account](https://dashboard.nutrient.io/sign_up/?product=processor) to get your API key.

> It’s now possible to [view and edit Word, Excel, and PowerPoint documents](https://www.nutrient.io/guides/web/viewer/office-documents.md) directly in the browser, without any server-side processing required.

## Why convert DOCX to PDF?

Converting DOCX files to PDF is essential for workflows requiring document standardization and sharing. Common use cases include:

- **Document preservation** — Convert Word documents to PDFs to preserve formatting, fonts, and layouts regardless of software or device used to view them.

- **Professional distribution** — Share contracts, reports, and proposals in a universal format that maintains consistent appearance across all platforms.

- **Compliance and archiving** — Create tamper-resistant records for legal compliance, audit trails, and long-term document storage.

- **Print-ready documents** — Ensure documents print exactly as intended without formatting issues or missing fonts.

- **Version control** — Lock document formatting to prevent accidental edits while allowing controlled review and approval processes.

The DOCX-to-PDF API automates this process in your workflow.

## Nutrient DWS Processor API

Converting DOCX to PDF is one of 30+ operations available through our [PDF API tools](https://www.nutrient.io/api/). Combine DOCX-to-PDF conversion with other tools for complex workflows:

- [Converting various file formats](https://www.nutrient.io/api/pdf-converter-api/) and consolidating them into PDFs

- Converting DOCX files and then [watermarking](https://www.nutrient.io/api/pdf-watermark-api/) and [flattening](https://www.nutrient.io/api/flatten-pdf-api/) the PDFs

- Processing multiple DOCX files before [merging them](https://www.nutrient.io/api/merge-pdf-api/) into a single PDF

Your account includes access to all PDF API tools.

## Step 1 — Creating a free account on Nutrient

Go to our [website](https://dashboard.nutrient.io/sign_up/?product=processor), where you’ll see the page below, prompting you to create your free account.![Free account Nutrient API](@/assets/images/blog/2022/how-to-merge-pdfs-using-python/image4.png)

Once you’ve created your account, you’ll be welcomed by a page showing an overview of your plan details.

You’ll start with 50 credits to process, and you’ll be able to access all our PDF API tools.

## Step 2 — Obtaining the API key

After you’ve verified your email, you can get your API key from the dashboard. In the menu on the left, click **API keys**. You’ll see the following page, which is an overview of your keys.![Convert DOCX to PDF Python API Key](@/assets/images/blog/2022/how-to-merge-pdfs-using-python/image1.png)

Copy the **Live API key**, because you’ll need this for the DOCX-to-PDF API.

## Step 3 — Setting up folders and files

Now, create a folder called `docx_to_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 `docx_to_pdf` and name them `input_documents` and `processed_documents`.

Copy your DOCX file to the `input_documents` folder and rename it to `document.docx`.

Then, in the root folder, `docx_to_pdf`, create a file called `processor.py`. This is the file where you’ll keep your code.

Your folder structure will look like this:

```

docx_to_pdf
├── input_documents
|    └── document.docx
├── processed_documents
└── processor.py

```

## Step 4 — Installing dependencies

Install the required `requests` library:

```bash

pip install requests

```

## Step 5 — Writing the code

Now, open the `processor.py` file and paste the code below into it:

```py

import requests
import json

response = requests.request(
  'POST',
  'https://api.nutrient.io/build',
  headers = {
    'Authorization': 'Bearer YOUR_API_KEY_HERE'
  },
  files = {
    'file': open('input_documents/document.docx', 'rb')
  },
  data = {
    'instructions': json.dumps({
      'parts': [
        {
          'file': 'file'
        }
      ]
    })
  },
  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()

```

Make sure to replace `YOUR_API_KEY_HERE` with your API key.

### Code explanation

The code imports the `requests` library for HTTP operations and `json` for formatting instructions. It creates a POST request to the Nutrient API with three key components:

- **headers** — Authorization with your API key.

- **files** — The input DOCX file as binary data.

- **data** — JSON instructions specifying how to process the file.

The response streams back as a PDF file, which the script saves to the `processed_documents` folder.

## Output

To execute the code, use the following command:

```bash

python3 processor.py

```

On the successful execution of the code, you’ll see a new processed file named `result.pdf` in the `processed_documents` folder.

The folder structure will look like this:

```

docx_to_pdf
├── input_documents
|    └── document.docx
├── processed_documents
|    └── result.pdf
└── processor.py

```

## Additional resources

Explore more ways to work with Nutrient API:

- **[Postman collection](https://www.nutrient.io/guides/dws-processor/getting-started/postman-collection.md)** — Test API endpoints directly in Postman

- **[Zapier integration](https://www.nutrient.io/guides/dws-processor/getting-started/zapier-integration.md)** — Automate document workflows without code

- **[MCP Server](https://www.nutrient.io/mcp-server-pdf-automation-llm)** — PDF automation for LLM applications

- **[Python examples](https://www.nutrient.io/guides/dws-processor/supported-languages/python.md)** — Official Python code examples and documentation

## Conclusion

This tutorial covered converting DOCX files to PDF documents in Python using the DOCX-to-PDF Python API.

You can integrate these functions into existing applications. The same API token enables other operations like merging documents, adding watermarks, and more. [Sign up](https://dashboard.nutrient.io/sign_up/?product=processor) for a free trial.

Want a broader view of the Python PDF ecosystem? Our [Python PDF library comparison](https://www.nutrient.io/blog/best-python-pdf-libraries/) covers seven tools for conversion, extraction, OCR, and signing.

## FAQ

#### Do I need a paid account to use Nutrient DWS Processor API?

No. Start with a free account that provides 50 credits for processing documents. Different operations consume different amounts of credits, so you can process varying numbers of documents depending on the operations you use.

#### Does Nutrient DWS Processor API preserve DOCX formatting when converting to PDF?

Yes. Nutrient DWS Processor API maintains document formatting, including fonts, styles, images, tables, and page layouts. Complex elements like headers, footers, and embedded objects are preserved in the PDF output.

#### What Python version is required for this code?

This code requires Python 3.7 or higher with the `requests` library installed. Install the requests library using `pip install requests`.

#### Can I convert multipage DOCX documents to PDF?

Yes. Nutrient DWS Processor API converts multipage DOCX documents to PDF while maintaining pagination, page breaks, and document structure across all pages.
---

## Related pages

- [The business case for accessibility: Five ways it drives enterprise value](/blog/5-ways-accessibility-drives-enterprise-value.md)
- [Accessibility Untangled Why It Matters Guide](/blog/accessibility-untangled-why-it-matters-guide.md)
- [Advanced Techniques For React Native Ui Components](/blog/advanced-techniques-for-react-native-ui-components.md)
- [`vector_store` holds your indexed documents (see the multimodal RAG post](/blog/agentic-rag.md)
- [Ai Document Automation Extraction To Action](/blog/ai-document-automation-extraction-to-action.md)
- [Ai Legal Assistant Document Authoring](/blog/ai-legal-assistant-document-authoring.md)
- [Angular File Viewer Pdf Image Office Files](/blog/angular-file-viewer-pdf-image-office-files.md)
- [Auto Tagging And Document Accessibility In Dotnet Sdk](/blog/auto-tagging-and-document-accessibility-in-dotnet-sdk.md)
- [Best Document Viewers](/blog/best-document-viewers.md)
- [The CEO’s AI playbook: Why decision architecture beats model selection](/blog/ceo-ai-playbook-decision-architecture.md)
- [1. Extract and chunk the PDF.](/blog/chat-with-pdf.md)
- [Complete Guide To Pdfjs](/blog/complete-guide-to-pdfjs.md)
- [Construction Document Data Extraction](/blog/construction-document-data-extraction.md)
- [Convert One Drive Files To Pdf In Sharepoint](/blog/convert-one-drive-files-to-pdf-in-sharepoint.md)
- [Create And Edit Pdfs In Flutter](/blog/create-and-edit-pdfs-in-flutter.md)
- [Create Pdfs With React](/blog/create-pdfs-with-react.md)
- [Creating A Document Scanner With Ocr In Python](/blog/creating-a-document-scanner-with-ocr-in-python.md)
- [Creating And Filling Pdf Forms Programmatically In Javascript](/blog/creating-and-filling-pdf-forms-programmatically-in-javascript.md)
- [The CTO’s AI playbook: Why accountability architecture beats orchestration](/blog/cto-ai-playbook-accountability-architecture.md)
- [Digital Signatures](/blog/digital-signatures.md)
- [Digital Workflow Automation](/blog/digital-workflow-automation.md)
- [Document Ai Vs Ocr](/blog/document-ai-vs-ocr.md)
- [Document Extraction Confidence Scores](/blog/document-extraction-confidence-scores.md)
- [Document Viewer](/blog/document-viewer.md)
- [Document Watermarking](/blog/document-watermarking.md)
- [Emerging threats: Your logging system may be an agentic threat vector](/blog/emerging-threats-your-logging-system.md)
- [app.py](/blog/extract-text-from-pdf-using-python.md)
- [Fillable Pdf](/blog/fillable-pdf.md)
- [How To Add Digital Signature To Pdf Using React](/blog/how-to-add-digital-signature-to-pdf-using-react.md)
- [How To Build A Dotnet Maui Pdf Viewer](/blog/how-to-build-a-dotnet-maui-pdf-viewer.md)
- [How To Build A Flutter Pdf Viewer](/blog/how-to-build-a-flutter-pdf-viewer.md)
- [or](/blog/how-to-build-a-javascript-pdf-viewer-with-pdfjs.md)
- [How To Build A Javascript Pdf Viewer](/blog/how-to-build-a-javascript-pdf-viewer.md)
- [or](/blog/how-to-build-a-nextjs-pdf-viewer.md)
- [How To Build A Powerpoint Viewer Using Javascript](/blog/how-to-build-a-powerpoint-viewer-using-javascript.md)
- [Using Yarn](/blog/how-to-build-a-react-excel-viewer.md)
- [How To Build A React Native Pdf Viewer](/blog/how-to-build-a-react-native-pdf-viewer.md)
- [How To Build A React Powerpoint Viewer](/blog/how-to-build-a-react-powerpoint-viewer.md)
- [How To Build A Reactjs File Viewer](/blog/how-to-build-a-reactjs-file-viewer.md)
- [or](/blog/how-to-build-a-reactjs-pdf-viewer-with-react-pdf.md)
- [or](/blog/how-to-build-a-reactjs-pdf-viewer.md)
- [How To Build A Reactjs Viewer With Pdfjs](/blog/how-to-build-a-reactjs-viewer-with-pdfjs.md)
- [How To Build A Vuejs Pdf Viewer With Pdfjs](/blog/how-to-build-a-vuejs-pdf-viewer-with-pdfjs.md)
- [How To Build A Vuejs Pdf Viewer](/blog/how-to-build-a-vuejs-pdf-viewer.md)
- [How To Build An Android Pdf Viewer](/blog/how-to-build-an-android-pdf-viewer.md)
- [How To Build An Angular Pdf Viewer With Ng2 Pdf Viewer](/blog/how-to-build-an-angular-pdf-viewer-with-ng2-pdf-viewer.md)
- [How To Build An Angular Pdf Viewer With Pdfjs](/blog/how-to-build-an-angular-pdf-viewer-with-pdfjs.md)
- [How To Convert Docx To Pdf Using Javascript](/blog/how-to-convert-docx-to-pdf-using-javascript.md)
- [How To Convert Html To Pdf Using Html2pdf](/blog/how-to-convert-html-to-pdf-using-html2pdf.md)
- [or](/blog/how-to-convert-html-to-pdf-using-react.md)
- [How To Convert Html To Pdf Using Wkhtmltopdf And Csharp](/blog/how-to-convert-html-to-pdf-using-wkhtmltopdf-and-csharp.md)
- [or](/blog/how-to-convert-html-to-pdf-using-wkhtmltopdf-and-python.md)
- [How To Convert Word To Pdf In Nodejs](/blog/how-to-convert-word-to-pdf-in-nodejs.md)
- [or](/blog/how-to-create-a-react-js-signature-pad.md)
- [How To Create Pdfs With React To Pdf](/blog/how-to-create-pdfs-with-react-to-pdf.md)
- [How To Edit Pdfs Using Ios Pdf Library](/blog/how-to-edit-pdfs-using-ios-pdf-library.md)
- [How To Embed A Pdf Viewer In Your Website](/blog/how-to-embed-a-pdf-viewer-in-your-website.md)
- [How To Extract Tables From Pdf And Images](/blog/how-to-extract-tables-from-pdf-and-images.md)
- [How To Generate Pdf From Html With Nodejs](/blog/how-to-generate-pdf-from-html-with-nodejs.md)
- [base_url tells WeasyPrint where to resolve relative asset paths](/blog/how-to-generate-pdf-reports-from-html-in-python.md)
- [How To Merge Pdfs Using Javascript](/blog/how-to-merge-pdfs-using-javascript.md)
- [How To Ocr Pdfs In Linux](/blog/how-to-ocr-pdfs-in-linux.md)
- [How To Print Pdf In Csharp](/blog/how-to-print-pdf-in-csharp.md)
- [Open an image.](/blog/how-to-use-tesseract-ocr-in-python.md)
- [From an HTML string.](/blog/html-in-pdf-format.md)
- [Javascript Pdf Editors](/blog/javascript-pdf-editors.md)
- [Javascript Pdf Libraries](/blog/javascript-pdf-libraries.md)
- [Linearized Pdf](/blog/linearized-pdf.md)
- [or](/blog/merge-pdfs.md)
- [Swift Package Manager](/blog/mobile-pdf-sdk.md)
- [`elements` come from your document parser — each has a type and content.](/blog/multimodal-rag.md)
- [Nutrient Vs Conga Composer](/blog/nutrient-vs-conga-composer.md)
- [Online Document Viewer](/blog/online-document-viewer.md)
- [Open Pdf In Your Web App](/blog/open-pdf-in-your-web-app.md)
- [Building WCAG 2.2, Section 508, and PDF/UA-compliant PDFs with an SDK](/blog/pdf-accessibility.md)
- [Pdf Extraction Benchmark Opendataloader Bench](/blog/pdf-extraction-benchmark-opendataloader-bench.md)
- [Pdf Extraction Document Case Studies](/blog/pdf-extraction-document-case-studies.md)
- [Pdf Page Labels](/blog/pdf-page-labels.md)
- [Pdf Sdk Compliance Security Checklist](/blog/pdf-sdk-compliance-security-checklist.md)
- [Pdf Sdk Performance Benchmark](/blog/pdf-sdk-performance-benchmark.md)
- [Pdf Ua Compliance Guide](/blog/pdf-ua-compliance-guide.md)
- [Pdfjs Accessibility Structtree Printing](/blog/pdfjs-accessibility-structtree-printing.md)
- [Pdfjs Advanced Loading Streaming Workers](/blog/pdfjs-advanced-loading-streaming-workers.md)
- [Pdfjs Annotation Editor Layer](/blog/pdfjs-annotation-editor-layer.md)
- [Pdfjs Area Annotations Canvas Capture](/blog/pdfjs-area-annotations-canvas-capture.md)
- [Pdfjs Coordinate Systems Pdf To Screen](/blog/pdfjs-coordinate-systems-pdf-to-screen.md)
- [Pdfjs Document Outline Bookmarks Metadata](/blog/pdfjs-document-outline-bookmarks-metadata.md)
- [Pdfjs Eventbus Guide](/blog/pdfjs-eventbus-guide.md)
- [macOS](/blog/pdfjs-generating-pdf-thumbnails-pdf2pic.md)
- [Pdfjs Limitations Commercial Upgrade](/blog/pdfjs-limitations-commercial-upgrade.md)
- [Pdfjs Native Annotation Layer Forms](/blog/pdfjs-native-annotation-layer-forms.md)
- [Pdfjs Navigation Zoom Rotation](/blog/pdfjs-navigation-zoom-rotation.md)
- [Pdfjs Pdf Page Manipulation Pdf Lib](/blog/pdfjs-pdf-page-manipulation-pdf-lib.md)
- [Pdfjs React Viewer Setup](/blog/pdfjs-react-viewer-setup.md)
- [Pdfjs Rendering Overlays React Portals](/blog/pdfjs-rendering-overlays-react-portals.md)
- [Pdfjs Server Side Text Extraction](/blog/pdfjs-server-side-text-extraction.md)
- [Pdfjs Sticky Note Annotations](/blog/pdfjs-sticky-note-annotations.md)
- [Pdfjs Text Highlight Annotations](/blog/pdfjs-text-highlight-annotations.md)
- [Pdfjs Text Search Pdffindcontroller](/blog/pdfjs-text-search-pdffindcontroller.md)
- [Pdfjs Thumbnail Sidebar](/blog/pdfjs-thumbnail-sidebar.md)
- [Process Flows](/blog/process-flows.md)
- [React Native Pdf Annotation](/blog/react-native-pdf-annotation.md)
- [Using Yarn](/blog/react-pdf-editor.md)
- [or](/blog/sample-blog-updated.md)
- [Sdk Product Updates Q2 2026](/blog/sdk-product-updates-q2-2026.md)
- [Add DWS MCP Server to your Claude Code project.](/blog/teaching-llms-to-read-pdfs.md)
- [Open an image file.](/blog/tesseract-python-guide.md)
- [Define the HTML part of the document.](/blog/top-10-ways-to-generate-pdfs-in-python.md)
- [Top 5 Javascript Pdf Viewers](/blog/top-5-javascript-pdf-viewers.md)
- [or](/blog/top-js-pdf-libraries.md)
- [Convert an HTML file to PDF.](/blog/top-ten-ways-to-convert-html-to-pdf.md)
- [Vector Pdf](/blog/vector-pdf.md)
- [Wcag2 Accessibility Requirements Documents](/blog/wcag2-accessibility-requirements-documents.md)
- [Web Sdk Is Now Headless](/blog/web-sdk-is-now-headless.md)
- [What Are Annotations](/blog/what-are-annotations.md)
- [What Is A Vpat](/blog/what-is-a-vpat.md)
- [What Is Document Processing](/blog/what-is-document-processing.md)
- [What Is Intelligent Document Processing](/blog/what-is-intelligent-document-processing.md)
- [What Is Pdf Ua](/blog/what-is-pdf-ua.md)
- [Why Pdfium Is A Trusted Platform For Pdf Rendering](/blog/why-pdfium-is-a-trusted-platform-for-pdf-rendering.md)
- [Why Your Ai Agent Hallucinates Pdf Table Data](/blog/why-your-ai-agent-hallucinates-pdf-table-data.md)

