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.
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-mvenv.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-mpipinstallgroupdocs-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.
importosfromgroupdocs.conversionimportLicense,Converterfromgroupdocs.conversion.options.convertimportPdfConvertOptionsdefconvert_docx_to_pdf():# Get license file absolute pathlicense_path=os.path.abspath("./GroupDocs.Conversion.lic")ifos.path.exists(license_path):# Create License and set the pathlicense=License()license.set_license(license_path)# Load DOCX filewithConverter("./business-plan.docx")asconverter:# Create convert optionspdf_convert_options=PdfConvertOptions()# Convert DOCX to PDFconverter.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.
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.
importosfromgroupdocs.conversionimportLicense,Converterfromgroupdocs.conversion.filetypesimportImageFileTypefromgroupdocs.conversion.options.convertimportImageConvertOptionsdefconvert_pdf_pages_to_png():# Get license file absolute pathlicense_path=os.path.abspath("./GroupDocs.Conversion.lic")ifos.path.exists(license_path):# Create License and set the pathlicense=License()license.set_license(license_path)output_folder="./converted-pages"os.makedirs(output_folder,exist_ok=True)# Load PDF documentwithConverter("./annual-review.pdf")asconverter:# Determine the total number of pages in the source documentpages_count=converter.get_document_info().pages_count# Create convert options and reuse them inside the looppng_convert_options=ImageConvertOptions()png_convert_options.format=ImageFileType.PNGpng_convert_options.pages_count=1# Convert each page to a separate PNG fileforpage_numberinrange(1,pages_count+1):png_convert_options.page_number=page_numberoutput_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.
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.
importosfromgroupdocs.conversionimportLicense,Converterfromgroupdocs.conversion.options.convertimportPdfConvertOptionsdefconvert_files_in_archive():# Get license file absolute pathlicense_path=os.path.abspath("./GroupDocs.Conversion.lic")ifos.path.exists(license_path):# Create License and set the pathlicense=License()license.set_license(license_path)# Load ZIP filewithConverter("./compressed.zip")asconverter:# Create convert optionspdf_convert_options=PdfConvertOptions()# Extract the archive, convert its contents, and save a consolidated PDFconverter.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.
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: