This guide provides a quick overview of how to set up and start using GroupDocs.Total for Python via .NET. It shows three code examples that demonstrate how to use packages included in the suite.
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-3.11-mvenv.venv
python3 -m venv .venv
Activate a virtual environment:
.venv\Scripts\activate
source .venv/bin/activate
Install groupdocs-total-net Package
After activating the virtual environment, run the following command in your terminal to install the latest version of the package. It installs all 14 products: about 2.2 GB to download and 3.2 GB on disk.
py-mpipinstallgroupdocs-total-net
python3 -m pip install groupdocs-total-net
Ensure the package is installed successfully. You should see the message
Successfully installed groupdocs-total-net-*
Example 1: Convert a document
This example shows how to convert a DOCX document to PDF using GroupDocs.Conversion.
You can download the demo app here.
importosfromgroupdocs.conversionimportLicense,Converterfromgroupdocs.conversion.options.convertimportPdfConvertOptionsdefconvert_docx_to_pdf():# Get license file absolute pathlicense_path=os.path.abspath("./GroupDocs.Total.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: Remove document metadata
This code example shows how to remove PDF document metadata and save the output to a file with GroupDocs.Metadata. You can download the app that we’re going to build here.
importosfromgroupdocs.metadataimportLicense,Metadatadefremove_pdf_metadata():# Get license file absolute pathlicense_path=os.path.abspath("./GroupDocs.Total.lic")ifos.path.exists(license_path):# Create License and set the pathlicense=License()license.set_license(license_path)# Load the PDF filewithMetadata("./business-plan.pdf")asmetadata:# Remove metadatametadata.sanitize()# Save the file with no metadatametadata.save("./no-metadata.pdf")if__name__=="__main__":remove_pdf_metadata()
business-plan.pdf is sample file used in this example. Click here to download it.
no-metadata.pdf is expected output PDF file without metadata. 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
Metadata("./business-plan.pdf"): Initializes metadata object with the source PDF file.
metadata.sanitize(): Removes the metadata.
metadata.save("./no-metadata.pdf"): Saves the output document without metadata.
Example 3: Convert and remove metadata
This example converts a DOCX file to PDF and removes metadata in a single workflow, using two products in one script.
You can download the demo app here.
importos# Windows: keep each product on its own .NET runtime (set before the first call)os.environ["DOTNET_MULTILEVEL_LOOKUP"]="0"fromgroupdocs.conversionimportLicenseasConversionLicense,Converterfromgroupdocs.conversion.options.convertimportPdfConvertOptionsfromgroupdocs.metadataimportLicenseasMetadataLicense,Metadatadefconvert_and_remove_metadata():# Get license file absolute pathlicense_path=os.path.abspath("./GroupDocs.Total.lic")ifos.path.exists(license_path):# License GroupDocs.Conversionconversion_license=ConversionLicense()conversion_license.set_license(license_path)# License GroupDocs.Metadatametadata_license=MetadataLicense()metadata_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)# Load created PDF filewithMetadata("./business-plan.pdf")asmetadata:# Remove the metadatametadata.sanitize()# Save the file with no metadatametadata.save("./converted-no-metadata.pdf")if__name__=="__main__":convert_and_remove_metadata()
business-plan.docx is sample file used in this example. Click here to download it.
converted-no-metadata.pdf is output PDF file without metadata. Click here to download the output PDF file.
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
os.environ["DOTNET_MULTILEVEL_LOOKUP"] = "0": On Windows, keeps each product on the .NET runtime it ships with. Without it, on a machine with a newer .NET installed system-wide, Viewer, Metadata, Annotation, Comparison, Editor, Merger and Redaction load that runtime instead, and the second of them in one script fails with RuntimeError: hostfxr_get_runtime_delegate failed with 0x800080a3. It has no effect on Linux and macOS.
Converter("./business-plan.docx"): Initializes the converter with the DOCX file.
converter.convert("./business-plan.pdf", pdf_convert_options): Converts the DOCX file to PDF and saves it as business-plan.pdf.
Metadata("./business-plan.pdf"): Reads converted PDF file.
metadata.sanitize(): Removes the metadata.
metadata.save("./converted-no-metadata.pdf"): Saves the output document without metadata.