Load Document from Local Disk

To load a source document from the local filesystem, pass a file path to the Merger class constructor. GroupDocs.Merger for Python via .NET provides several constructor overloads to cover different loading scenarios:

  • Merger(file_path) — open a document by path with default settings.
  • Merger(file_path, load_options) — open a document by path and supply additional loading options (e.g. a password for protected files).
  • Merger(file_path, load_options, settings) — open a document by path with load options and custom MergerSettings.

You can use either an absolute or a relative path. If the specified path does not exist, an exception is raised when the first operation is performed.

Tip
GroupDocs.Merger accesses the file only when an action (e.g. get_document_info(), join(), save()) is performed on the Merger instance.

The following example demonstrates loading a PDF document from a local path and inspecting its metadata:

from groupdocs.merger import Merger

def load_from_local_disk():
    # Provide a relative or absolute path to the source document
    with Merger("./input.pdf") as merger:
        # Retrieve document information to confirm the file loaded correctly
        info = merger.get_document_info()
        print("Format:", info.type.file_format)
        print("Pages:", info.page_count)
        print("Size:", info.size, "bytes")

if __name__ == "__main__":
    load_from_local_disk()

input.pdf is a sample file used in this example. Click here to download it.

Format: Portable Document Format File
Pages: 2
Size: 86913 bytes

Download full output

Explanation

  • File Path: The Merger constructor accepts the path "./input.pdf" and stores it internally. The file is not opened until an action is performed.
  • Context Manager: The with statement ensures that all unmanaged resources (file handles, .NET objects) are released automatically when the block exits, even if an exception occurs.
  • Inspect Metadata: merger.get_document_info() opens the file, reads its metadata, and returns an IDocumentInfo object. The example prints the human-readable format name (info.type.file_format), total page count, and file size in bytes.
Warning
GroupDocs.Merger determines the file type by its extension. Make sure the source file has the correct extension; otherwise the library may misdetect the format.

Refer to the GroupDocs.Merger API Reference for full details on the Merger constructor overloads.

See also