Convert Word Documents to PDF using REST API in Python.

It is a common practice to share Word documents in a PDF format because PDF is a widely used document-sharing format in the industry. You can easily convert Word to PDF using the built-in functionality provided by Microsoft Office, but you may need to convert your Word documents (DOC or DOCX) into PDF programmatically. In this article, you will learn how to convert Word documents to PDF using a REST API in Python.

The following topics shall be covered in this article:

Word to PDF Conversion REST API and Python SDK

For converting DOCX to PDF, we will be using the Python SDK of GroupDocs.Conversion Cloud API. It is a platform-independent document/image conversion solution and has no dependency on any tool or software. It enables you to quickly and reliably convert images and documents of any supported file format to any format you need.

You can install GroupDocs.Conversion Cloud to your Python application using the following command in the console:

pip install groupdocs_conversion_cloud

Please get your Client ID and Secret from the dashboard before following the mentioned steps. Once you have your ID and secret, add in the code as shown below:

Convert Word Documents to PDF using a REST API in Python

You can convert your Word documents to PDF programmatically on the cloud by following the simple steps given below:

  1. Upload the DOCX file to the cloud
  2. Convert DOCX to PDF using Python
  3. Download the converted PDF file

Upload the DOCX File

Firstly, upload the DOCX file to the cloud using the following code sample:

As a result, the uploaded DOCX file will be available in the files section of your dashboard on the cloud.

Convert DOCX to PDF using Python

You can easily convert DOCX to PDF document programmatically by following the steps mentioned below:

  • Firstly, create an instance of the ConvertApi.
  • Now, create an instance of the ConvertSettings.
  • Then, provide the input DOCX file path.
  • Set format as the “pdf”.
  • Provide the output file path.
  • Now, create ConvertDocumentRequest with ConvertSettings.
  • Finally, convert DOCX by calling the convert_document() method with ConvertDocumentRequest.

The following code example shows how to convert DOCX to PDF using a REST API in Python.

Convert Word Documents to PDF using a REST API in Python

Convert Word Documents to PDF using a REST API in Python.

Download the Converted File

The above code sample will save the converted PDF file on the cloud. You can download it using the following code sample:

Word to PDF Conversion with Advanced Options

You can convert Word documents to PDF files with some advanced settings by following the steps given below:

  • Firstly, create an instance of the ConvertApi.
  • Now, create an instance of the ConvertSettings.
  • Then, provide the DOCX file path.
  • Set the “pdf” as format.
  • Provide the output file path.
  • Now, create an instance of the DocxLoadOptions
  • Optionally set various load options such as hide_comments, hide_word_tracked_changes, etc.
  • Now, create an instance of the PdfConvertOptions
  • Then, set various convert options such as display_doc_title, margins (top, left, right, bottom), etc.
  • Now, create ConvertDocumentRequest with ConvertSettings
  • Finally, convert DOCX by calling the convert_document() method with ConvertDocumentRequest

The following code example shows how to convert a Word document to a PDF document with advanced convert options. Please follow the steps mentioned earlier to upload and download a file.

Convert Range of Pages from DOCX to PDF in Python

You can convert a range of pages from a Word document to a PDF file programmatically by following the steps mentioned below:

  • Firstly, create an instance of the ConvertApi
  • Now, create an instance of the ConvertSettings
  • Then, provide the input DOCX file path
  • Assign “pdf” to the format
  • Provide the output file path
  • Now, create an instance of the PdfConvertOptions
  • Then, provide a page range to convert from start page number and total pages to convert
  • Now, assign _PdfConvertOptions_ to ConvertSettings
  • Then, create ConvertDocumentRequest with ConvertSettings
  • Finally, convert by calling the convert_document() method with ConvertDocumentRequest

The following code sample shows how to convert a range of pages from a DOCX to PDF using a REST API in Python. Please follow the steps mentioned earlier to upload and download a file.

Convert Specific Pages of DOCX to PDF in Python

You can convert specific pages of a Word document to a PDF file programmatically by following the steps mentioned below:

  • Firstly, create an instance of the ConvertApi
  • Now, create an instance of the ConvertSettings
  • Then, provide the input DOCX file path
  • Assign “pdf” to the format
  • Provide the output file path
  • Now, create an instance of the PdfConvertOptions
  • Then, provide specific page numbers in a comma-separated array to convert
  • Now, assign _PdfConvertOptions_ to ConvertSettings
  • Then, create ConvertDocumentRequest with ConvertSettings
  • Finally, convert by calling the convert_cocument() method with ConvertDocumentRequest

The following code example shows how to convert specific pages of a Word document to PDF using a REST API in Python. Please follow the steps mentioned earlier to upload and download a file.

Word to PDF Conversion with Watermark using Python

You can convert Word documents to PDF documents and add watermarks to converted documents programmatically by following the steps given below:

  • Firstly, create an instance of the ConvertApi
  • Now, create an instance of the ConvertSettings
  • Then, provide the input DOCX file path
  • Assign “pdf” to the format
  • Provide the output file path
  • Now, create an instance of the WatermarkOptions
  • Then, set Watermark Text, Color, Width, Height, Left, Top, etc.
  • Now, define the PdfConvertOptions and assign WatermarkOptions
  • Now, create ConvertDocumentRequest with ConvertSettings
  • Finally, convert DOCX by calling the convert_document() method with ConvertDocumentRequest

The following code example shows how to convert a Word document to a PDF document and add a watermark to the converted PDF document using a REST API in Python. Please follow the steps mentioned earlier to upload and download files.

Word to PDF Conversion with Watermark using Python.

Word to PDF Conversion with Watermark using Python.

DOCX to PDF Conversion without using Cloud Storage

You can convert a Word document to PDF without using the cloud storage by passing it in the request body and receiving the output file in the API response. Please follow the steps mentioned below to convert a DOCX to a PDF without using cloud storage.

  • Firstly, create an instance of the ConvertApi
  • Read input DOCX file from local path
  • Now, create ConvertDocumentDirectRequest
  • Then, provide the target format as “pdf” and the input file path as input parameters
  • Get results by calling the convert_document_d_irect()_ method with ConvertDocumentDirectRequest
  • Finally, save the output file to the local path using FileStream.writeFile() method

The following code example shows how to convert a Word document to a PDF without using cloud storage.

Convert DOCX to PDF and Download Directly

You can convert DOCX to PDF documents programmatically and download the converted file directly by following the steps given below:

  • Firstly, create an instance of ConvertApi
  • Now, create an instance of the ConvertSettings
  • Then, set the DOCX file path
  • Assign “pdf” to the format
  • Set “None” to the output path
  • Now, create ConvertDocumentRequest with ConvertSettings
  • Then, get results by calling the convert_document_download() method
  • Optionally, save the output file to the local path using shutil.move() method

The following code example shows how to convert a DOCX file to a PDF document and download it directly using a REST API in Python. The API shall return the converted PDF file in response. Please follow the steps mentioned earlier to upload a file.

Try Online

Please try the following free online DOCX to PDF conversion tool, which is developed using the above API. https://products.groupdocs.app/conversion/docx-to-pdf

Conclusion

In this article, you have learned how to convert Word documents to PDF files on the cloud. You have also seen how to convert specific pages or a range of pages from a DOCX to a PDF using Python. This article also explained how to programmatically upload the DOCX file on the cloud and then download the converted PDF file from the cloud. Besides, you can learn more about GroupDocs.Conversion Cloud API using the documentation. We also provide an API Reference section that lets you visualize and interact with our APIs directly through the browser. In case of any ambiguity, please feel free to contact us on the forum.

See Also