在 Python 中合并 Excel 文件

合并 Excel 文件是各个行业中的常见任务。企业通常需要合并来自多个来源的数据以进行分析或报告。当您合并 Excel 文件时,您可以简化工作流程。这个过程节省时间并减少错误。Python 开发人员可以轻松自动化此任务。在这篇博客文章中,我们将探讨如何使用 Aspose.Cells for Python 合并 Excel 文件。

本文涵盖以下主题:

合并 Excel 文件的 Python 库

Aspose.Cells for Python 是一个强大的库,可以简化合并 Excel 文件的过程。它提供了一个强大的 API 用于处理电子表格。您可以轻松地以编程方式操作、创建和合并 Excel 文件。Aspose.Cells 支持多种 Excel 格式,使其在不同应用中具有多功能性。其高效处理大型数据集的能力是开发人员的一大优势。

Aspose.Cells for Python 提供了几个使其理想用于合并 Excel 文件的功能:

  • 集成简便:该库与 Python 应用程序无缝集成。
  • 灵活性:支持多种 Excel 格式,包括 XLSX、XLS 和 CSV。
  • 高级自定义:您可以自定义数据合并的方式,包括格式和布局选项。
  • 性能:高效处理大型文件,确保快速处理时间。

要开始使用 Aspose.Cells for Python,您需要安装该库。您可以从 releases 下载它,或使用以下 pip 命令进行安装:

pip install aspose-cells-python

如何在 Python 中合并两个 Excel 文件

请按照以下步骤使用 Aspose.Cells for Python 在 Python 中合并两个 Excel 文件:

  1. 使用 Workbook 类对象加载 Excel 文件。
  2. 调用 combine() 方法合并文件。
  3. 使用 save() 方法将合并的工作簿保存到新文件中。

以下是实现这些步骤的 Python 代码片段:

import aspose.cells as cells
# Load the first Excel file
book1 = cells.Workbook("book1.xlsx")
# Load the second Excel file
book2 = cells.Workbook("book2.xlsx")
# Merge Files
book1.combine(book2)
# Save Merged File
book1.save("merged-excel-file.xlsx")

在 Python 中合并多个 Excel 文件

要在 Python 中合并两个以上的 Excel 文件作为替代方法,请按照以下步骤操作:

  1. 创建一个 Excel 文件数组。
  2. 创建一个缓存文件以进行处理。
  3. 使用 CellsHelper.merge_files() 方法将文件合并到输出文件中。
  4. 使用 Workbook 类加载输出文件以重命名工作表。
  5. 循环遍历所有工作表以相应地重命名它们。
  6. 使用 save() 方法重新保存工作簿。

以下是实现这些步骤的 Python 代码片段:

import aspose.cells as cells
from aspose.cells import CellsHelper
# Create an Array of Excel files
files = ["input.xlsx", "Book1.xlsx", "Book2.xlsx"]
# Create a cachedFile for the process
cacheFile = "test.txt"
# Output File to be created
dest = "output.xlsx"
# Merge the files in the output file. Supports only .xls files
CellsHelper.merge_files(files, cacheFile, dest)
# Now if you need to rename your sheets, you may load the output file
workbook = cells.Workbook("output.xlsx")
i = 1
# Browse all the sheets to rename them accordingly
for sheet in workbook.worksheets:
sheet.name = "Sheet10" + str(i)
i
i = i + 1
# Re-save the file
workbook.save("output.xlsx")

使用 Python 将多个工作表合并为一个

请按照以下简单步骤使用 Aspose.Cells 在 Python 中将多个 Excel 工作表合并为一个:

  1. 使用 Workbook 类加载包含多个工作表的 Excel 文件。
  2. 创建一个新的 Workbook 以在单个工作表中存储合并的数据。
  3. 循环遍历每个工作表。
  4. 读取每个工作表的数据范围。
  5. 使用 copy() 方法将所有行复制到目标工作表中。
  6. 跟踪行号以避免覆盖。
  7. 使用 save() 方法保存新文件。
import aspose.cells as cells
# Load the Excel file
workbook = cells.Workbook("input.xlsx")
# Create a new workbook for the merged data
dest_workbook = cells.Workbook()
dest_sheet = dest_workbook.worksheets.get(0)
total_row_count = 0
is_first_sheet = True # Flag to track the first sheet
for source_sheet in workbook.worksheets:
source_range = source_sheet.cells.max_display_range
if source_range is None or source_range.row_count == 0:
continue # Skip empty sheets
start_row = 0 if is_first_sheet else 1 # Skip header for subsequent sheets
rows_to_copy = source_range.row_count - start_row
if rows_to_copy > 0:
source_data_range = source_sheet.cells.create_range(
source_range.first_row + start_row, source_range.first_column,
rows_to_copy, source_range.column_count
)
dest_data_range = dest_sheet.cells.create_range(
total_row_count, source_range.first_column, rows_to_copy, source_range.column_count
)
dest_data_range.copy(source_data_range)
total_row_count += rows_to_copy # Increment row count
is_first_sheet = False # Mark first sheet as processed
# Save the combined sheet
dest_workbook.save("combinedSheets.xlsx")

获取免费许可证

有兴趣探索 Aspose 产品吗?访问 许可证页面 获取免费临时许可证。开始很简单,您可以解锁 Aspose.Cells for Python 的全部潜力。

在线合并 Excel 文件

您还可以 在线合并 Excel 文件。这个工具是免费的,易于使用,允许您快速合并文件而无需安装任何软件。

合并 Excel 文件:免费资源

除了合并 Excel 文件外,我们还提供各种资源,以增强您对 Aspose.Cells for Python 的理解。查看我们的文档和教程以获取更多见解和示例。

结论

在这篇博客文章中,我们探讨了如何使用 Aspose.Cells for Python 合并 Excel 文件。我们强调了该库的功能,并提供了合并两个和多个 Excel 文件的代码示例。如果您想简化 Excel 文件管理,请考虑进一步了解 Aspose.Cells for Python。

如果您有任何问题或需要进一步的帮助,请随时通过我们的 免费支持论坛 联系我们。

另请参阅