Merge HTML

GroupDocs.Merger for Python via .NET allows you to combine multiple HTML files into a single document programmatically. All content — text, images, tables, embedded styles, and other markup — is preserved in the merged output.

HTML (HyperText Markup Language) is the standard language for web pages displayed in browsers. HTML 5, the latest version, offers broad flexibility for structuring and presenting content. HTML files can be served from a web server or loaded from the local file system.

Steps to merge HTML files

  1. Create an instance of the Merger class and pass the path to the first (base) HTML file.
  2. Call merger.join() with the path to each additional HTML file to append.
  3. Call merger.save() with the desired output path to write the merged file.
from groupdocs.merger import Merger

def merge_html_documents():
    # Load the first HTML file as the merge base
    with Merger("./input.html") as merger:
        # Append the second HTML file
        merger.join("./input2.html")
        # Save the combined HTML file
        merger.save("./output.html")

if __name__ == "__main__":
    merge_html_documents()

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

input2.html is a sample file used in this example. Click here to download it.

<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><meta http-equiv="Content-Style-Type" content="text/css" /><meta name="generator" content="Aspose.Words for .NET 26.4.0" /><title></title></head><body style="font-family:'Times New Roman'; font-size:12pt"><div><p style="margin-top:0pt; margin-bottom:0pt"><a name="Bookmark1"><span>Text inside a bookmark 1</span></a></p><p style="margin-top:12pt; margin-bottom:3pt; page-break-after:avoid; font-size:16pt; -aw-outline-l
[TRUNCATED]

Download full output

Explanation

  • Load base document: Merger("./input.html") opens the first HTML file as the merge base inside a context manager.
  • Append second document: merger.join("./input2.html") appends the content of the second file. Call join again for each additional file.
  • Save result: merger.save("./output.html") writes the merged HTML to disk.

Merge HTML Live Demo

GroupDocs.Merger for Python via .NET provides an online HTML Merger App, which allows you to try it for free and check its quality and accuracy.

API reference

  • Merger — main class; join, save methods.

See also