Loading Documents

GroupDocs.Viewer for Python via .NET can load a source document from three kinds of sources: a local file path, a Python stream (file handle or BytesIO), or an HTTP URL. Pass any of them to the Viewer class — the engine auto-detects the format and keeps the source open only as long as necessary.

Viewer Class Constructor

The Viewer constructor accepts the source as its first positional argument. The remaining arguments are all optional and can be passed positionally or as keyword arguments:

Viewer(source, load_options=None, settings=None, leave_open=None)
  • source — a file path (str), raw bytes, or any file-like stream (open("file", "rb"), io.BytesIO(data)).
  • load_options — a LoadOptions instance. Use it to pass a password, override the detected file type, or tune resource-loading behaviour.
  • settings — a ViewerSettings instance. Holds the logger and cache, applied to the whole rendering pipeline. See Logging and Diagnostics.
  • leave_open — when True, the Viewer will not close the underlying stream after rendering. Handy when you want to keep reading from the same handle afterwards.

Always construct the Viewer inside a with block. The context manager releases the native handle and any cached render state as soon as the block exits.

Load Options

LoadOptions is a single class whose properties cover every format the Viewer supports. Only set the properties that apply to your source — the rest default sensibly.

PropertyTypePurpose
file_typeFileTypeOverride the detected format. Useful when a file has no extension or an ambiguous MIME type.
passwordstrPassword for an encrypted document (DOCX, XLSX, PDF, archive, Outlook, …).
encodingstrForce an encoding for plain-text and CSV sources.
detect_encodingboolEnable automatic encoding detection for text.
try_repairboolAttempt to recover data from a corrupted source.
resource_loading_timeouttimedeltaUpper bound on external-resource fetches (HTML / MHTML / EPUB / SVG).
skip_external_resourcesboolRender without fetching any external resources.
whitelisted_resourceslist[str]When skip_external_resources is True, allow this specific list through.

Examples

Note
The three pages in this section each cover one source type — local disk, stream, and URL. For the full runnable examples, see the companion GroupDocs.Viewer for Python via .NET examples repository.
Close
Loading

Analyzing your prompt, please hold on...

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