Merge Excel spreadsheets

View as MarkdownMarkdown

Set up your AI agent

Give your coding agent live access to the GroupDocs documentation and API reference through the GroupDocs Docs MCP server — no install, no API key. Learn more

claude mcp add --transport http groupdocs-docs https://docs.groupdocs.com/mcp

Also process documents locally

The GroupDocs.Merger MCP server lets your agent work with files on your machine; they are never uploaded. It needs the .NET 10 SDK or Docker.

claude mcp add groupdocs-merger -- dnx GroupDocs.Merger.Mcp --yes
Folders, licensing and more →

GroupDocs.Merger for Python via .NET allows you to combine multiple Excel spreadsheets into a single file programmatically. All worksheets, data, formulas, charts, and formatting are preserved — no Microsoft Excel or third-party software required.

Spreadsheet files store data in rows and columns and are saved in formats such as XLSX (Microsoft Excel Open XML Spreadsheet), XLS (Microsoft Excel Binary File Format), and ODS (OpenDocument Spreadsheet). XLSX is the default format introduced with Microsoft Office 2007, based on the Open Packaging Conventions (OOXML/ECMA-376 standard).

Steps to merge XLSX files

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

def merge_excel_documents():
    # Load the first document as the merge base
    with Merger("./input.xlsx") as merger:
        # Append the second Excel spreadsheet
        merger.join("./input2.xlsx")
        # Save the combined document
        merger.save("./output.xlsx")

if __name__ == "__main__":
    merge_excel_documents()

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

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

Binary file (XLSX, 17 KB)

Download full output

Explanation

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

Merge rows of several spreadsheets into one worksheet

By default, each joined spreadsheet adds its worksheets to the result. To append the rows of the joined spreadsheets below the existing data instead, for example to collect monthly ledgers into one table, pass SpreadsheetJoinOptions with mode set to SpreadsheetJoinMode.ROWS:

  1. Create an instance of the Merger class and pass the path to the first spreadsheet. This document is always taken in full.
  2. Create a SpreadsheetJoinOptions, set mode to SpreadsheetJoinMode.ROWS and, if the joined files have header rows, set skip_rows to the number of rows to skip.
  3. Call merger.join() with each other spreadsheet and the options. The rows of each file are appended below the last row with data in the matching worksheet.
  4. Call merger.save() with the path of the merged spreadsheet.
from groupdocs.merger import Merger
from groupdocs.merger.domain.options import SpreadsheetJoinMode, SpreadsheetJoinOptions

def merge_spreadsheet_rows():
    # Load the first spreadsheet
    with Merger("./january.xlsx") as merger:
        # Append rows instead of adding worksheets, and skip the header row of each joined file
        join_options = SpreadsheetJoinOptions()
        join_options.mode = SpreadsheetJoinMode.ROWS
        join_options.skip_rows = 1
        merger.join("./february.xlsx", join_options)
        merger.join("./march.xlsx", join_options)
        # Save the merged spreadsheet
        merger.save("./q1.xlsx")

if __name__ == "__main__":
    merge_spreadsheet_rows()

january.xlsx is a sample file used in this example. Click here to download it.

february.xlsx is a sample file used in this example. Click here to download it.

march.xlsx is a sample file used in this example. Click here to download it.

Binary file (XLSX, 10 KB)

Download full output

Explanation

  • SpreadsheetJoinOptions(): join settings for spreadsheets. mode decides what a join adds: worksheets (SpreadsheetJoinMode.WORKSHEETS, the default) or rows (SpreadsheetJoinMode.ROWS).
  • skip_rows = 1: leaves out the first row of each joined file, here the header row that the first spreadsheet already has.
  • One options object for every join: the same SpreadsheetJoinOptions is passed to both join calls.

Matching worksheets

When the spreadsheets have several worksheets, the sheet_matching property (SpreadsheetSheetMatching) decides where the rows go:

  • SpreadsheetSheetMatching.BY_INDEX (the default): the rows of the n-th worksheet of a joined file are appended to the n-th worksheet of the result. Worksheets beyond the number of result worksheets are added as new worksheets.
  • SpreadsheetSheetMatching.FIRST_SHEET_ONLY: only the first worksheet of each joined file is appended, to the first worksheet of the result.

skip_rows and sheet_matching have no effect when mode is SpreadsheetJoinMode.WORKSHEETS.

What is carried over

Row-wise joining carries cell values, formulas (relative references are adjusted to the new position), cell styles, merged cells and row heights. Charts, pictures and other floating objects, pivot tables, tables, conditional formatting and data validation of the joined files are not carried over.

Row-wise joining works for XLSX, XLS, XLSM, XLSB, XLTX, XLTM, XLT, XLAM and ODS, also when a joined spreadsheet has a different format than the first one (for example, XLS joined into XLSX).

Warning
  • A negative skip_rows makes join raise GroupDocsMergerException (from groupdocs.merger.exceptions).
  • Appending more rows than the output format allows (65,536 rows for XLS and XLT, 1,048,576 rows for the other formats) raises GroupDocsMergerException.
  • apply_page_builder raises GroupDocsMergerException while a spreadsheet joined in ROWS mode is pending, because appended rows do not form separate pages.

Merge XLSX Live Demo

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

API reference

See also