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
Create an instance of the Merger class and pass the path to the first (base) XLSX file.
Call merger.join() with the path to each additional XLSX file to append.
Call merger.save() with the desired output path to write the merged file.
fromgroupdocs.mergerimportMergerdefmerge_excel_documents():# Load the first document as the merge basewithMerger("./input.xlsx")asmerger:# Append the second Excel spreadsheetmerger.join("./input2.xlsx")# Save the combined documentmerger.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.
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:
Create an instance of the Merger class and pass the path to the first spreadsheet. This document is always taken in full.
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.
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.
Call merger.save() with the path of the merged spreadsheet.
fromgroupdocs.mergerimportMergerfromgroupdocs.merger.domain.optionsimportSpreadsheetJoinMode,SpreadsheetJoinOptionsdefmerge_spreadsheet_rows():# Load the first spreadsheetwithMerger("./january.xlsx")asmerger:# Append rows instead of adding worksheets, and skip the header row of each joined filejoin_options=SpreadsheetJoinOptions()join_options.mode=SpreadsheetJoinMode.ROWSjoin_options.skip_rows=1merger.join("./february.xlsx",join_options)merger.join("./march.xlsx",join_options)# Save the merged spreadsheetmerger.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.
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.