在Python中将数据从一个Excel文件复制到另一个

Excel workbooks are a major source of data collections. Python programming language provides few libraries to perform operations on the excel workbooks, like copying the data from one workbook to another.

Excel工作簿是数据收集的主要来源。 Python编程语言提供了很少的库来对excel工作簿执行操作,例如将数据从一个工作簿复制到另一个工作簿

Note: The library mentioned below needs to be installed using pip in the virtual environment.

注意:下面提到的库需要在虚拟环境中使用pip安装。

openpyxl (openpyxl)

https://pypi.org/project/openpyxl/

https://pypi.org/project/openpyxl/

Openpyxl is a python library to read/write Excel files (xlsx, xlsm, xltx, xltm). This library provides an option to read every cell of the workbook and either copies it or modify it by using openpyxl.worksheet.Worksheet.cell() method. This method allows accessing each cell by the row and column as a numerical value.

Openpyxl是用于读取/写入Excel文件(xlsx,xlsm,xltx,xltm)的python库。 该库提供了一个选项,用于读取工作簿的每个单元格,然后使用openpyxl.worksheet.Worksheet.cell()方法对其进行复制或修改。 此方法允许按行和列访问每个单元格作为数值。

The below example, will demonstrate the process of copying the data from a source excel file to the destination file, by row/column.

下面的示例将演示按行/列将数据从源excel文件复制到目标文件的过程

Step 1: Consider a source workbook, say source.xlsx and destination workbook, say destination.xlsx. The latter file is empty to where the contents of the former file will be copied. Below is the example of the former file (source.xlsx).

步骤1:考虑一个源工作簿,例如source.xlsx和目标工作簿,例如destination.xlsx。 后一个文件为空,将复制前一个文件的内容。 下面是前一个文件(source.xlsx)的示例。

Working with excel in Python Example

Step 2: Load the workbooks

步骤2:加载工作簿

    from openpyxl import load_workbook

    src_wb = load_workbook('source.xlsx')
    dest_wb = load_workbook('destination.xlsx')

Step 3: Read the sheets to be copied

步骤3:阅读要复制的图纸

    src_sheet = src_wb.get_sheet_by_name('source_sheet')
    dest_sheet = dest_wb.get_sheet_by_name('destination')

Step 4: Copy all the rows and columns

步骤4:复制所有行和列

for i in range(1, src_sheet.max_row+1):
    for j in range(1, src_sheet.max_column+1):
        dest_sheet.cell(row=i, column=j).value = src_sheet.cell(row=i, column=j).value

Step 5: Save the workbooks

步骤5:保存工作簿

    src_wb.save('source.xlsx')
    dest_wb.save('destination.xlsx')

The contents from the source is now copied and saved in the destination file.

现在,将复制源中的内容并将其保存在目标文件中。

Python代码将数据从一个Excel文件复制到另一个 (Python code to Copy data from one excel file to another)

from openpyxl import load_workbook

src_wb = load_workbook('source.xlsx')
dest_wb = load_workbook('destination.xlsx')

src_sheet = src_wb.get_sheet_by_name('source_sheet')
dest_sheet = dest_wb.get_sheet_by_name('destination')

for i in range(1, src_sheet.max_row+1):
    for j in range(1, src_sheet.max_column+1):
        dest_sheet.cell(row=i, column=j).value = src_sheet.cell(row=i, column=j).value

src_wb.save('source.xlsx')
dest_wb.save('destination.xlsx')


翻译自: https://www.includehelp.com/python/copy-data-from-one-excel-file-to-another.aspx

  • 10
    点赞
  • 86
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
回答: 要在Python中将文件复制到另一个文件夹,可以使用shutil模块的copytree函数。该函数的用法是shutil.copytree(files, little_sample_file),其files是原文件夹的路径,little_sample_file是新文件夹的路径,复制过来的文件将存储在该文件夹下。\[1\]另外,你也可以使用shutil模块的copy函数复制单个文件。例如,使用shutil.copy(src, dst)函数,其src是原文件的路径,dst是目标文件夹的路径。\[2\]如果你想一次性复制整个文件的所有文件,你可以使用os模块和shutil模块的组合。例如,使用os.listdir(src)遍历原文件文件,然后使用shutil.copy函数将每个文件复制到目标文件。\[3\] #### 引用[.reference_title] - *1* [【Python】将文件复制到另一个文件夹,将文件复制到新的位置](https://blog.csdn.net/ncc1995/article/details/94358099)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [Python—将一个文件复制到另一个文件以及合并两个文件](https://blog.csdn.net/weixin_45652976/article/details/122459170)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [python怎么把一个文件夹内的文件复制到另外一个文件夹](https://blog.csdn.net/qq_27328197/article/details/118299569)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值