Render to HTML

When rendering to HTML, GroupDocs.Viewer renders each page of the source document as a separate HTML document.

GroupDocs.Viewer for Python provides two options to manage CSS, fonts, images, and other resources:

  1. HTML with external resources stores page resources as separate files. This allows reusing common resources and results in reducing page size and loading speed.
  2. HTML with embedded resources integrates page resources into HTML. This makes each document page self-sufficient but results in increasing page size and loading speed.

To render files to HTML, follow these steps:

  1. Create an instance of the Viewer class. Specify the source document path as a constructor parameter.
  2. Instantiate the HtmlViewOptions object. Specify a path to save the rendered pages.
  3. Call the View.view() method of the Viewer object. Specify the HtmlViewOptions object as the parameter.

Rendering to HTML with external resources

The following code snippet shows how to render a .docx document to HTML with external resources:

from groupdocs.viewer import Viewer
from groupdocs.viewer.options import HtmlViewOptions

def render_to_html_external():
    # Create an HTML file for each page.
    # Specify the HTML file names and location of external resources.
    # {0} and {1} are replaced with the current page number and resource name, respectively.
    with Viewer("sample.docx") as viewer:
        html_options = HtmlViewOptions.for_external_resources("page_{0}.html", "page_{0}/resource_{0}_{1}", "page_{0}/resource_{0}_{1}")
        viewer.view(html_options)

if __name__ == "__main__":
    render_to_html_external()

sample.docx is the sample file used in this example. Click here to download it.

Rendering to HTML with embedded resources

The following code snippet shows how to render a .docx document to HTML with embedded resources:

from groupdocs.viewer import Viewer
from groupdocs.viewer.options import HtmlViewOptions

def render_to_html_embedded():
    # Create an HTML file for each page.
    # {0} is replaced with the current page number in the file name.
    with Viewer("sample.docx") as viewer:
        html_options = HtmlViewOptions.for_embedded_resources("page_{0}.html")
        viewer.view(html_options)

    print(f"\nSource document rendered successfully.")

if __name__ == "__main__":
    render_to_html_embedded()

sample.docx is the sample file used in this example. Click here to download it.

For details, please refer to the following pages:

    Close
    Loading

    Analyzing your prompt, please hold on...

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