openpyxl xlsx文件复制一列到另一列
时间: 2023-11-08 16:03:58 浏览: 98
可以使用openpyxl库来实现将一个列复制到另一个列。下面是一个示例代码:
```python
from openpyxl import Workbook, load_workbook
def copy_column(source_file, source_sheet, source_column, destination_file, destination_sheet, destination_column):
# 加载源文件和目标文件
source_workbook = load_workbook(source_file)
destination_workbook = load_workbook(destination_file)
# 获取源工作表和目标工作表
source_worksheet = source_workbook[source_sheet]
destination_worksheet = destination_workbook[destination_sheet]
# 遍历源列,并将数据复制到目标列
for index, cell in enumerate(source_worksheet[source_column]):
destination_worksheet[destination_column + str(index+1)].value = cell.value
# 保存目标文件
destination_workbook.save(destination_file)
# 示例用法
copy_column("source.xlsx", "Sheet1", "A", "destination.xlsx", "Sheet2", "B")
```
注意,在运行上述代码之前,请确保已经安装了openpyxl库。
阅读全文