使用 Python 在 Excel 中复制行和列

liftword2个月前 (03-01)技术文章21

在 Excel 中复制行和列是一项重要的技术,可让您快速复制和重用数据。您无需多次手动输入相同的信息,只需选择所需的行或列,然后使用简单的复制命令即可创建准确的副本。

复制行和列在各种情况下都很有用。当您需要创建数据备份时,复制行和列可让您轻松生成数据副本,从而确保数据完整性。此外,当您需要在工作表中移动数据或执行数据计算时,复制行和列使您能够灵活地操作和分析数据,而无需更改原始数据。

用于在 Excel 中复制行和列的 Python 库

要使用 Python 在 Excel 中复制行和列,我们可以使用 Spire.XLS for Python 库。

Spire.XLS for Python 是一个易于使用且功能丰富的库,用于在 Python 应用程序中创建、读取、编辑和转换 Excel 文件。使用此库,您可以处理许多电子表格格式,例如 XLS、XLSX、XLSB、XLSM 和 ODS。此外,您还可以将 Excel 文件渲染为其他类型的文件格式,例如 PDF、HTML、CSV、文本、图像、XML、SVG、ODS、PostScript 和 XPS。

您可以通过在终端中运行以下命令从 PyPI 安装 Spire.XLS for Python:

pip install Spire.Xls

有关安装的更多详细信息,您可以查看此官方文档:如何在 VS Code 中安装 Spire.XLS for Python。

使用 Python 在 Excel 中复制单行和单列

您可以使用 Worksheet.CopyRow(sourceRow, destSheet, destRowIndex, copyOptions)Worksheet.CopyColumn(sourceColumn, destSheet, destColIndex, copyOptions) 函数轻松复制特定的行或列。copyOptions 参数允许您为正在复制的行或列指定其他复制选项。这些选项包括仅复制公式值、复制样式、复制所有属性等。

下面是一个简单的示例,演示如何使用 Python 和 Spire.XLS for Python 在 Excel 中复制特定的行和列:

from spire.xls import *
from spire.xls.common import *

# Instantiate a Workbook object
workbook = Workbook()
# Load an Excel file
workbook.LoadFromFile("Sample.xlsx")

# Get the first worksheet
source_sheet = workbook.Worksheets[0]
# Get the second worksheet
dest_sheet = workbook.Worksheets[1]

# Get the first row of the first worksheet by its index (0-based) and copy it to the first row of the second worksheet
source_sheet.CopyRow(source_sheet.Rows[0], dest_sheet, 1, CopyRangeOptions.All)

# Get the first column of the first worksheet by its index (0-based) and copy it to the first column of the second worksheet
source_sheet.CopyColumn(source_sheet.Columns[0], dest_sheet, 1, CopyRangeOptions.All)

# Save the resulting file
workbook.SaveToFile("CopySingleRowAndColumn.xlsx", ExcelVersion.Version2016)
workbook.Dispose()

使用 Python 在 Excel 中复制多行和多列

有时,您可能希望在 Excel 工作表中复制多行或多列。在 Spire.XLS for Python 中,可以根据指定的范围复制多行或多列。

下面是一个简单的示例,演示如何使用 Python 和 Spire.XLS for Python 在 Excel 中复制多个行和列:

from spire.xls import *
from spire.xls.common import *

# Instantiate a Workbook object
workbook = Workbook()
# Load an Excel file
workbook.LoadFromFile("Sample.xlsx")

# Get the first worksheet
source_sheet = workbook.Worksheets[0]
# Get the second worksheet
dest_sheet = workbook.Worksheets[1]

# Copy the first 3 rows from the first worksheet to the second worksheet by specifying the corresponding cell range
source_sheet.CopyRow(source_sheet.Range["A1:C3"], dest_sheet, 1, CopyRangeOptions.All)

# Copy the first 2 columns from the first worksheet to the second worksheet by specifying the corresponding cell range
source_sheet.CopyColumn(source_sheet.Range["A1:B11"], dest_sheet, 1, CopyRangeOptions.All)

# Save the resulting file
workbook.SaveToFile("CopyMultipleRowsAndColumns.xlsx", ExcelVersion.Version2016)
workbook.Dispose()

使用 Python 仅在 Excel 中复制可见行和列

在 Excel 中复制可见的行和列是一项必不可少的技术,它使您能够复制数据,同时排除任何隐藏的行和列。

通过利用 Worksheet.GetRowIsHide(rowIndex)Worksheet.GetColumnIsHide(colIndex) 方法,您可以毫不费力地确定工作表中各个行和列的可见性状态。

下面是一个简单的示例,演示如何使用 Python 和 Spire.XLS for Python 在 Excel 中复制可见行或列:

from spire.xls import *
from spire.xls.common import *

# Instantiate a Workbook object
workbook = Workbook()
# Load an Excel file
workbook.LoadFromFile("Sample.xlsx")

# Get the first worksheet
source_sheet = workbook.Worksheets[0]
# Get the second worksheet
dest_sheet = workbook.Worksheets[1]

# Copy visible rows in the worksheet
dest_row_index = 1
# Iterate through the used rows in the worksheet
for i in range(0, source_sheet.LastRow):
    # Find the visible rows
    if not source_sheet.GetRowIsHide(i + 1):
        # Copy the visible rows from the first worksheet to the second worksheet
        source_sheet.CopyRow(source_sheet.Rows[i], dest_sheet, dest_row_index, CopyRangeOptions.All)
        dest_row_index += 1

# # Or copy visible columns in the worksheet
# dest_col_index = 1
# # Iterate through the used columns in the worksheet
# for j in range(0, source_sheet.LastColumn):
#     # Find the visible columns
#     if not source_sheet.GetColumnIsHide(j + 1):
#         # Copy the visible columns from the first worksheet to the second worksheet
#         source_sheet.CopyColumn(source_sheet.Columns[j], dest_sheet, dest_col_index, CopyRangeOptions.All)
#         dest_col_index += 1
        
# Save the resulting file
workbook.SaveToFile("CopyVisibleRows.xlsx", ExcelVersion.Version2016)
workbook.Dispose()

相关文章

用Python复制文件的9个方法_python怎么复制文件到另一个地方

Python 中有许多“开盖即食”的模块(比如 os,subprocess 和 shutil)以支持文件 I/O 操作。在这篇文章中,你将会看到一些用 Python 实现文件复制的特殊方法。下面我们开...

文件备份用Python,照着复制粘贴代码就可以了

引言在日常开发和运维工作中,数据安全尤为重要,定期备份重要文件是防范数据丢失的有效手段之一。本文将详细介绍如何使用Python实现一个简单的定时备份脚本,该脚本可以按照设定周期自动将指定文件夹或文件复...

Python浅拷贝深拷贝之copy、deepcopy

笔记记录20221205:个人总结:1,两者基本区别不大;2,在涉及到子对象时候,两者才有区别;3,在涉及到子对象,且子对象的操作后内存地址没有发生变化(如下方代码:dic1['one'].appen...

Python之掌握文件删除、复制、重命名及内容查找的技巧

亲爱的读者们,今天我们将为大家详细介绍Python中文件操作的四大技巧:文件删除、文件复制、文件重命名以及文件内容查找。掌握这些技巧,将有助于你在编程过程中更加高效地处理文件。一、文件的删除在Pyth...

python散装笔记——76: 数据拷贝_python数据传输

1: 拷贝一个字典字典对象具有复制方法。它执行字典的浅层复制。˃˃˃ d1 = {1:[]} ˃˃˃ d2 = d1.copy() ˃˃˃ d1 is d2 False ˃˃˃ d1[1] is d2...

Python 初学者练习:复制文件_python中的复制

在本教程中,您将学习如何使用 os、shutil 模块中提供的各种函数将文件和文件夹从一个位置复制到另一个位置。在 Python 中使用 copy() 复制文件复制文件可以使用 shutil 模块的...