Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Convert Word Documents to PDF using REST API in Python

Learn how to Convert Word Documents to PDF using REST API in Python: https://blog.groupdocs.cloud/2021/11/12/convert-word-documents-to-pdf-using-rest-api-in-python/

The following topics are covered in the article:

  1. Convert Word Documents to PDF using a REST API in Python
  2. Word to PDF Conversion with Advanced Options
  3. Convert Range of Pages from DOCX to PDF in Python
  4. Convert Specific Pages of DOCX to PDF in Python
  5. Word to PDF Conversion with Watermark using Python
  6. DOCX to PDF Conversion without using Cloud Storage
  7. Convert DOCX to PDF and Download Directly
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 = ""
# 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\\")
# 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)
# 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)
# 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)
# 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)
# 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\\")
# 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)
# 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\\")
# 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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment