Quick Start Guide

This guide provides a quick overview of how to set up and start using GroupDocs.Conversion for Python via .NET. This library enables developers to convert between various file formats (e.g., DOCX, PDF, PNG) with minimal configuration.

Prerequisites

To proceed, make sure you have:

  1. Configured environment as described in the System Requirements topic.
  2. Optionally you may Get a Temporary License to test all the product features.

Set Up Your Development Environment

For best practices, use a virtual environment to manage dependencies in Python applications. Learn more about virtual environment at Create and Use Virtual Environments documentation topic.

Create and Activate a Virtual Environment

Create a virtual environment:

py -m venv .venv
python3 -m venv .venv
python3 -m venv .venv

Activate a virtual environment:

.venv\Scripts\activate
source .venv/bin/activate
source .venv/bin/activate

Install groupdocs-conversion-net Package

After activating the virtual environment, run the following command in your terminal to install the latest version of the package:

py -m pip install groupdocs-conversion-net
python3 -m pip install groupdocs-conversion-net
python3 -m pip install groupdocs-conversion-net

Ensure the package is installed successfully. You should see the message

Successfully installed groupdocs-conversion-net-*

Example 1: Convert document

To quickly test the library, let’s convert a DOCX file to PDF. You can also download the app that we’re going to buid here.

import os
from groupdocs.conversion import License, Converter
from groupdocs.conversion.options.convert import PdfConvertOptions

def convert_docx_to_pdf():
    # Get license file absolute path
    license_path = os.path.abspath("./GroupDocs.Conversion.lic")

    if os.path.exists(license_path):
        # Create License and set the path
        license = License()
        license.set_license(license_path)

    # Load DOCX file
    with Converter("./business-plan.docx") as converter:
        # Create convert options
        pdf_convert_options = PdfConvertOptions()

        # Convert DOCX to PDF
        converter.convert("./business-plan.pdf", pdf_convert_options)    

if __name__ == "__main__":
    convert_docx_to_pdf()

business-plan.docx is sample file used in this example. Click here to download it.

Binary file (PDF, 283 KB)

Download full output

Your folder tree should look similar to the following directory structure:

📂 demo-app
 ├──convert_docx_to_pdf.py
 ├──business-plan.docx
 └──GroupDocs.Conversion.lic (Optionally)

Run the App

py convert_docx_to_pdf.py
python3 convert_docx_to_pdf.py
python3 convert_docx_to_pdf.py

After running the app you can deactivate virtual environment by executing deactivate or closing your shell.

Explanation

  • Converter("./business-plan.docx"): Initializes the converter with the DOCX file.
  • PdfConvertOptions(): Specifies the output format as PDF.
  • converter.convert("./business-plan.pdf", pdf_convert_options): Converts the DOCX file to PDF and saves it as business-plan.pdf.

Example 2: Convert document pages

In this example we’ll convert PDF document pages to PNG. You can download the app that we’re going to buid here.

import os
from groupdocs.conversion import License, Converter
from groupdocs.conversion.filetypes import ImageFileType
from groupdocs.conversion.options.convert import ImageConvertOptions

def convert_pdf_pages_to_png():
    # Get license file absolute path
    license_path = os.path.abspath("./GroupDocs.Conversion.lic")

    if os.path.exists(license_path):
        # Create License and set the path
        license = License()
        license.set_license(license_path)

    output_folder = "./converted-pages"
    os.makedirs(output_folder, exist_ok=True)

    # Load PDF document
    with Converter("./annual-review.pdf") as converter:
        # Determine the total number of pages in the source document
        pages_count = converter.get_document_info().pages_count

        # Create convert options and reuse them inside the loop
        png_convert_options = ImageConvertOptions()
        png_convert_options.format = ImageFileType.PNG
        png_convert_options.pages_count = 1

        # Convert each page to a separate PNG file
        for page_number in range(1, pages_count + 1):
            png_convert_options.page_number = page_number
            output_file = os.path.join(output_folder, f"converted-page-{page_number}.png")
            converter.convert(output_file, png_convert_options)

if __name__ == "__main__":
    convert_pdf_pages_to_png()

annual-review.pdf is sample file used in this example. Click here to download it.

converted-pages/converted-page-1.png (1148 KB)
converted-pages/converted-page-2.png (89 KB)
converted-pages/converted-page-3.png (83 KB)

Download full output

Your folder tree should look similar to the following directory structure:

📂 demo-app
 ├──annual-review.pdf
 ├──convert_pdf_pages_to_png.py
 └──GroupDocs.Conversion.lic (Optionally)

Run the App

py convert_pdf_pages_to_png.py
python3 convert_pdf_pages_to_png.py
python3 convert_pdf_pages_to_png.py

After running the app you can deactivate virtual environment by executing deactivate or closing your shell.

Explanation

  • Converter("./annual-review.pdf"): Initializes the converter with the PDF file.
  • converter.get_document_info().pages_count: Retrieves the total number of pages in the source document.
  • ImageConvertOptions() with format = ImageFileType.PNG: Specifies the output format as PNG image.
  • The loop updates png_convert_options.page_number on each iteration (with pages_count = 1) and calls converter.convert(...) to write one PNG file per page into the converted-pages folder.

Example 3: Convert files in archive

In this example we’ll convert the contents of a ZIP archive to PDF. GroupDocs.Conversion opens the archive, converts the files inside, and produces a single consolidated PDF that contains every converted document. You can download the app that we’re going to buid here.

import os
from groupdocs.conversion import License, Converter
from groupdocs.conversion.options.convert import PdfConvertOptions

def convert_files_in_archive():
    # Get license file absolute path
    license_path = os.path.abspath("./GroupDocs.Conversion.lic")

    if os.path.exists(license_path):
        # Create License and set the path
        license = License()
        license.set_license(license_path)

    # Load ZIP file
    with Converter("./compressed.zip") as converter:
        # Create convert options
        pdf_convert_options = PdfConvertOptions()

        # Extract the archive, convert its contents, and save a consolidated PDF
        converter.convert("./converted.pdf", pdf_convert_options)

if __name__ == "__main__":
    convert_files_in_archive()

compressed.zip is sample file used in this example. Click here to download it.

Binary file (PDF, 283 KB)

Download full output

Your folder tree should look similar to the following directory structure:

📂 demo-app
 ├──compressed.zip
 ├──convert_files_in_archive.py
 └──GroupDocs.Conversion.lic (Optionally)

Run the App

py convert_files_in_archive.py
python3 convert_files_in_archive.py
python3 convert_files_in_archive.py

After running the app you can deactivate virtual environment by executing deactivate or closing your shell.

Explanation

  • Converter("./compressed.zip"): Initializes the converter with the ZIP file.
  • PdfConvertOptions(): Specifies the output format as PDF.
  • converter.convert("./converted.pdf", pdf_convert_options): Extracts the archive, converts its contents, and writes a single consolidated PDF to converted.pdf.

Next Steps

After completing the basics, explore additional resources to enhance your usage:

Close
Loading

Analyzing your prompt, please hold on...

An error occurred while retrieving the results. Please refresh the page and try again.