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
Activate a virtual environment:
.venv\Scripts\activate
source .venv/bin/activate
Install GroupDocs.Conversion Package
After activating the virtual environment, copy the downloaded OS-specific package into your project directory and install it.
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.
business-plan.pdf is expected output PDF file. Click here to download it.
Your folder tree should look similar to the following directory structure:
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)# Load PDF documentwithConverter("./annual-review.pdf")asconverter:# Create convert optionspng_convert_options=ImageConvertOptions()png_convert_options.format=ImageFileType.PNG# Convert document pages and save converted pages in the output folderconverter.convert_by_page("./converted-pages",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 is output folder where converted pages are stored. Click here to download the output PNG files.
Your folder tree should look similar to the following directory structure:
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.
ImageConvertOptions(): Specifies the output format as image.
ImageFileType.PNG: Sets the output image format to PNG.
converter.convert_by_page("./converted-pages", png_convert_options): Converts the PDF file pages to PNG and saves output file in converted-pages folder.
Example 3: Convert files in archive
In this example we’ll convert documents packed into ZIP archive to PDF. 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 ZIP and convert each file to PDFconverter.convert_multiple("./converted-files",pdf_convert_options)if__name__=="__main__":convert_files_in_archive()
compressed.zip is sample file used in this example. Click here to download it.
converted-files is output folder where files converted to PDF are stored. Click here to download the output PDF files.
Your folder tree should look similar to the following directory structure:
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 for files as PDF.
converter.convert_multiple("./converted-files", pdf_convert_options): Extracts files from ZIP, converts each file to PDF saves output files in converted-files folder.
Next Steps
After completing the basics, explore additional resources to enhance your usage: