iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >python 实现从一个excel表格中选择相应区域数据粘贴到另一个excel表格中指定区域的方法
  • 191
分享到

python 实现从一个excel表格中选择相应区域数据粘贴到另一个excel表格中指定区域的方法

python 2023-09-04 16:09:50 191人浏览 安东尼

Python 官方文档:入门教程 => 点击学习

摘要

import pandas as pdfrom openpyxl import load_workbookdef qyhhsj(source_file,source_sheetname,source_col,source_start_row

import pandas as pdfrom openpyxl import load_workbookdef qyhhsj(source_file,source_sheetname,source_col,source_start_row,source_end_row,source_start_col,source_end_col,target_file,target_sheetname,target_start_row,target_end_row,target_start_col,target_end_col):    """    source_file  数据源表格所在的路径    source_sheetname   数据源表格所在的sheet名称    source_col     数据源表格中需要的数据列的范围,如从C列到S列,则写成C:S    source_start_row  数据源表格开始行    source_end_row   数据源表格结束行    source_start_col 数据源表格开始列    source_end_col  数据源表格结束列    target_file          目标表格所在的路径    target_sheetname    目标表格所在的sheet名称    target_start_row     目标表格开始行    target_end_row    目标表格结束行    target_start_col    目标表格开始列    target_end_col    目标表格结束列    """    # 读取excel文件    file_path = source_file    df = pd.read_excel(file_path, sheet_name=source_sheetname, header=None, index_col=None, usecols=source_col)    # 打开需要替换数据的Excel文件    wb2 = load_workbook(target_file)    # 获取第二个Excel文件中指定的sheet    ws2 = wb2[target_sheetname]    # 选择要交换的区域数据    required_data = df.iloc[source_start_row:source_end_row, source_start_col:source_end_col]    # 打印显示required_data中第一行第一列的数据    # print(required_data.iloc[0, 0])    """    # 指定要写入的Excel区域    target_start_row = 5  # 区域从第5行开始    target_start_col = 2  # 区域从第2列开始    target_end_row = 17  # 区域从第17行结束    target_end_col = 19  # 区域从第19列结束    """    ks_row=target_start_row    ks_col=target_start_col    # 遍历需要替换的区域行和列,将每个单元格的值替换成替换数据列中对应行的值    for i in range(target_start_row, target_end_row):        for j in range(target_start_col, target_end_col):            print(i, j)            # 将数据写入第i行第j列            ws2.cell(row=i, column=j, value=required_data.iloc[i - ks_row, j - ks_col])    # 保存修改后的第一个Excel文件    wb2.save(target_file)#调用qyhhsj方法,从源表格的第C列第1行到第13行数据输入到目标表格的'水平位移'sheet中第2列第5行到第17行中qyhhsj( r'C:\Users\Thinkpad\Desktop\数据最终处理结果(人工监测)\X位移量合并.xls','Sheet1','C:S',1,13,0,17,r'C:\Users\Thinkpad\Desktop\数据最终处理结果(人工监测)\金溪水库资料整编2023110--V6.xlsx','水平位移',5,17,2,19)#qyhhsj( r'C:\Users\Thinkpad\Desktop\数据最终处理结果(人工监测)\Y位移量合并.xls','Sheet1','C:S',1,13,0,17,r'C:\Users\Thinkpad\Desktop\数据最终处理结果(人工监测)\金溪水库资料整编2023110--V6.xlsx','水平位移',5,17,24,41)

来源地址:https://blog.csdn.net/ducanwang/article/details/131716830

--结束END--

本文标题: python 实现从一个excel表格中选择相应区域数据粘贴到另一个excel表格中指定区域的方法

本文链接: https://www.lsjlt.com/news/393915.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作