This guide provides a quick overview of how to set up and start using GroupDocs.Viewer for Python via .NET. This library enables developers to render documents to HTML, PDF, PNG, and JPEG formats 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:
python3 -m venv .venv
python3 -m venv .venv
py-mvenv.venv
Activate a virtual environment:
source .venv/bin/activate
source .venv/bin/activate
.venv\Scripts\activate
Install groupdocs-viewer-net Package
After activating the virtual environment, run the following command in your terminal to install the latest version of the package:
python3 -m pip install groupdocs-viewer-net
python3 -m pip install groupdocs-viewer-net
py-mpipinstallgroupdocs-viewer-net
Ensure the package is installed successfully. You should see the message
Successfully installed groupdocs-viewer-net-*
Example 1: Render document to HTML
To quickly test the library, let’s render a DOCX file to HTML. You can also download the app that we’re going to build here.
importosfromgroupdocs.viewerimportLicense,Viewerfromgroupdocs.viewer.optionsimportHtmlViewOptionsdefrender_docx_to_html():# Get license file absolute pathlicense_path=os.path.abspath("./GroupDocs.Viewer.lic")ifos.path.exists(license_path):# Create License and set the pathlicense=License()license.set_license(license_path)# Load DOCX filewithViewer("./sample.docx")asviewer:# Create view optionshtml_options=HtmlViewOptions.for_embedded_resources("render_docx_to_html/page_{0}.html")# Render DOCX to HTMLviewer.view(html_options)if__name__=="__main__":render_docx_to_html()
sample.docx is sample file used in this example. 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
Viewer("./sample.docx"): Initializes the viewer with the DOCX file.
HtmlViewOptions.for_embedded_resources("render_docx_to_html/page_{0}.html"): Specifies the output format as HTML with embedded resources.
viewer.view(html_options): Renders the DOCX file to HTML and saves it as page_1.html, page_2.html, etc.
Example 2: Render document to PDF
In this example we’ll render a DOCX file to PDF. You can download the app that we’re going to build here.
importosfromgroupdocs.viewerimportLicense,Viewerfromgroupdocs.viewer.optionsimportPdfViewOptionsdefrender_docx_to_pdf():# Get license file absolute pathlicense_path=os.path.abspath("./GroupDocs.Viewer.lic")ifos.path.exists(license_path):# Create License and set the pathlicense=License()license.set_license(license_path)# Load DOCX filewithViewer("./sample.docx")asviewer:# Create view optionspdf_options=PdfViewOptions("render_docx_to_pdf/output.pdf")# Render DOCX to PDFviewer.view(pdf_options)if__name__=="__main__":render_docx_to_pdf()
sample.docx is sample file used in this example. 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
Viewer("./sample.docx"): Initializes the viewer with the DOCX file.
PdfViewOptions("output.pdf"): Specifies the output format as PDF.
viewer.view(pdf_options): Renders the DOCX file to PDF and saves it as output.pdf.
Example 3: Render document to PNG images
In this example we’ll render a DOCX file to PNG images. You can download the app that we’re going to build here.
importosfromgroupdocs.viewerimportLicense,Viewerfromgroupdocs.viewer.optionsimportPngViewOptionsdefrender_docx_to_png():# Get license file absolute pathlicense_path=os.path.abspath("./GroupDocs.Viewer.lic")ifos.path.exists(license_path):# Create License and set the pathlicense=License()license.set_license(license_path)# Load DOCX filewithViewer("./sample.docx")asviewer:# Create view optionspng_options=PngViewOptions("render_docx_to_png/output_{0}.png")# Render DOCX to PNG imagesviewer.view(png_options)if__name__=="__main__":render_docx_to_png()
sample.docx is sample file used in this example. Click here to download it.
Your folder tree should look similar to the following directory structure: