Hello, World!

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.

Prerequisites

To proceed, make sure you have:

  1. Configured environment as described in the System Requirements topic.
  2. Optionally you may Get a Temporary License to test all the product features.

Set Up Your Development Environment

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 -m venv .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 -m pip install groupdocs-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.

import os
from groupdocs.viewer import License, Viewer
from groupdocs.viewer.options import HtmlViewOptions

def render_docx_to_html():
    # Get license file absolute path
    license_path = os.path.abspath("./GroupDocs.Viewer.lic")

    if os.path.exists(license_path):
        # Create License and set the path
        license = License()
        license.set_license(license_path)

    # Load DOCX file
    with Viewer("./sample.docx") as viewer:
        # Create view options
        html_options = HtmlViewOptions.for_embedded_resources("render_docx_to_html/page_{0}.html")

        # Render DOCX to HTML
        viewer.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:

📂 demo-app
 ├──render_docx_to_html.py
 ├──sample.docx
 └──GroupDocs.Viewer.lic (Optionally)

Run the App

python3 render_docx_to_html.py
python3 render_docx_to_html.py
py render_docx_to_html.py

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.

import os
from groupdocs.viewer import License, Viewer
from groupdocs.viewer.options import PdfViewOptions

def render_docx_to_pdf():
    # Get license file absolute path
    license_path = os.path.abspath("./GroupDocs.Viewer.lic")

    if os.path.exists(license_path):
        # Create License and set the path
        license = License()
        license.set_license(license_path)

    # Load DOCX file
    with Viewer("./sample.docx") as viewer:
        # Create view options
        pdf_options = PdfViewOptions("render_docx_to_pdf/output.pdf")

        # Render DOCX to PDF
        viewer.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:

📂 demo-app
 ├──sample.docx
 ├──render_docx_to_pdf.py
 └──GroupDocs.Viewer.lic (Optionally)

Run the App

python3 render_docx_to_pdf.py
python3 render_docx_to_pdf.py
py render_docx_to_pdf.py

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.

import os
from groupdocs.viewer import License, Viewer
from groupdocs.viewer.options import PngViewOptions

def render_docx_to_png():
    # Get license file absolute path
    license_path = os.path.abspath("./GroupDocs.Viewer.lic")

    if os.path.exists(license_path):
        # Create License and set the path
        license = License()
        license.set_license(license_path)

    # Load DOCX file
    with Viewer("./sample.docx") as viewer:
        # Create view options
        png_options = PngViewOptions("render_docx_to_png/output_{0}.png")

        # Render DOCX to PNG images
        viewer.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:

📂 demo-app
 ├──sample.docx
 ├──render_docx_to_png.py
 └──GroupDocs.Viewer.lic (Optionally)

Run the App

python3 render_docx_to_png.py
python3 render_docx_to_png.py
py render_docx_to_png.py

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.
  • PngViewOptions("render_docx_to_png/output_{0}.png"): Specifies the output format as PNG images.
  • viewer.view(png_options): Renders each page of the DOCX file to PNG and saves them as output_1.png, output_2.png, etc.

Next Steps

After completing the basics, explore additional resources to enhance your usage:

Close
Loading

Analyzing your prompt, please hold on...

An error occurred while retrieving the results. Please refresh the page and try again.