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:

client_id = "659fe7da-715b-4744-a0f7-cf469a392b73"
client_secret = "b377c36cfa28fa69960ebac6b6e36421"
configuration = groupdocs_conversion_cloud.Configuration(client_id, client_secret)
configuration.api_base_url = "https://api.groupdocs.cloud"
my_storage = ""

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:

# Create instance of the API
file_api = groupdocs_conversion_cloud.FileApi.from_config(configuration)
# upload file request
request = groupdocs_conversion_cloud.UploadFileRequest("sample.docx", "C:\\Files\\Conversion\\sample.docx", my_storage)
# upload sample file
response = file_api.upload_file(request)

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.

# Create an instance of the API
convert_api = groupdocs_conversion_cloud.ConvertApi.from_keys(client_id, client_secret)
# Define convert settings
settings = groupdocs_conversion_cloud.ConvertSettings()
settings.file_path = "sample.docx"
settings.format = "pdf"
settings.output_path = "converted"
# Create convert document request
request = groupdocs_conversion_cloud.ConvertDocumentRequest(settings)
# Convert document
result = convert_api.convert_document(request)
# Done
print("Document converted: " + result[0].path)
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:

# API initialization
file_api = groupdocs_conversion_cloud.FileApi.from_config(configuration)
# Create download file request
request = groupdocs_conversion_cloud.DownloadFileRequest("converted\\sample.pdf", my_storage)
# Download file
response = file_api.download_file(request)
# Move downloaded file to your working directory
shutil.move(response, "C:\\Files\\Conversion\\")

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.

# Create an instance of the API
convert_api = groupdocs_conversion_cloud.ConvertApi.from_keys(client_id, client_secret)
# Define convert settings
settings = groupdocs_conversion_cloud.ConvertSettings()
settings.file_path = "sample.docx"
settings.format = "pdf"
settings.output_path = "converted"
# DOCX load options
loadOptions = groupdocs_conversion_cloud.DocxLoadOptions()
loadOptions.hide_comments = True # Hide comments
loadOptions.hide_word_tracked_changes = True # Hide tracked changes
# PDF convert options
convertOptions = groupdocs_conversion_cloud.PdfConvertOptions()
convertOptions.center_window = True
convertOptions.compress_images = False
convertOptions.display_doc_title = True
convertOptions.dpi = 1024.0
convertOptions.fit_window = False
convertOptions.from_page = 1
convertOptions.grayscale = False
convertOptions.image_quality = 100
convertOptions.linearize = False
convertOptions.margin_top = 5
convertOptions.margin_left = 5
convertOptions.unembed_fonts = True
convertOptions.remove_unused_streams = True
convertOptions.remove_unused_objects = True
convertOptions.remove_pdfa_compliance = False
settings.convert_options = convertOptions
# Convert document request
request = groupdocs_conversion_cloud.ConvertDocumentRequest(settings)
# Convert document
result = convert_api.convert_document(request)
# Done
print("Document converted: " + result[0].path)

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.

# Create an instance of API
convert_api = groupdocs_conversion_cloud.ConvertApi.from_keys(client_id, client_secret)
# Define convert settings
settings = groupdocs_conversion_cloud.ConvertSettings()
settings.file_path = "sample.docx"
settings.format = "pdf"
settings.output_path = "converted"
# PDF convert options
convertOptions = groupdocs_conversion_cloud.PdfConvertOptions()
convertOptions.from_page = 1; # start page number
convertOptions.pages_count = 2; # total pages to convert
settings.convert_options = convertOptions
# Convert document request
request = groupdocs_conversion_cloud.ConvertDocumentRequest(settings)
# Convert document
result = convert_api.convert_document(request)
# Done
print("Document converted: " + result[0].path)

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.

# Create an instance of API
convert_api = groupdocs_conversion_cloud.ConvertApi.from_keys(client_id, client_secret)
# Define convert settings
settings = groupdocs_conversion_cloud.ConvertSettings()
settings.file_path = "sample.docx"
settings.format = "pdf"
settings.output_path = "converted"
# PDF convert options
convertOptions = groupdocs_conversion_cloud.PdfConvertOptions()
convertOptions.pages = [1,2]; # page numbers to convert
settings.convert_options = convertOptions
# Convert document request
request = groupdocs_conversion_cloud.ConvertDocumentRequest(settings)
# Convert document
result = convert_api.convert_document(request)
# Done
print("Document converted: " + result[0].path)

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.

# API initialization
convert_api = groupdocs_conversion_cloud.ConvertApi.from_keys(client_id, client_secret)
# Define convert settings
settings = groupdocs_conversion_cloud.ConvertSettings()
settings.file_path = "sample.docx"
settings.format = "pdf"
settings.output_path = "converted"
# Define watermark options
watermark = groupdocs_conversion_cloud.WatermarkOptions()
watermark.text = "THIS IS SAMPLE WATERMARK"
watermark.bold = True;
watermark.font_size = 34;
watermark.color = "Gray";
watermark.background = False;
watermark.rotation_angle = 30;
watermark.left = 90;
watermark.top = 500;
# Define PDF convert options
convertOptions = groupdocs_conversion_cloud.PdfConvertOptions()
convertOptions.watermark_options = watermark
settings.convert_options = convertOptions
# Create convert document request
request = groupdocs_conversion_cloud.ConvertDocumentRequest(settings)
# Convert document
result = convert_api.convert_document(request)
print("Document converted successfully: " + result[0].url)
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.

# Create an instance of API
convert_api = groupdocs_conversion_cloud.ConvertApi.from_keys(client_id, client_secret)
# Create convert request
request = groupdocs_conversion_cloud.ConvertDocumentDirectRequest("pdf", "C:\\Files\\Conversion\\sample.docx")
# Convert
result = convert_api.convert_document_direct(request)
# Move downloaded file to your working directory
shutil.move(result, "C:\\Files\\Conversion\\")

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.

# Create necessary API instances
convert_api = groupdocs_conversion_cloud.ConvertApi.from_keys(client_id, client_secret)
# Define convert settings
settings = groupdocs_conversion_cloud.ConvertSettings()
settings.file_path = "sample.docx"
settings.format = "pdf"
settings.output_path = None # leave OutputPath will result the output as document IOStream
# Create convert request
request = groupdocs_conversion_cloud.ConvertDocumentRequest(settings)
# Convert
response = convert_api.convert_document_download(request)
# Move downloaded file to your working directory
shutil.move(response, "C:\\Files\\Conversion\\")

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