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
Activate a virtual environment:
.venv\Scripts\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:
py-mpipinstallgroupdocs-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 buid 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.
You can download the demo app here.
importosfromgroupdocs.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: