Python操作Excel,读写

2 篇文章 0 订阅

用到的库:xlrd

python版本:3.10

需求:

1文件为目标文件,2文件为处理文件。2文件中有多个sheet,1文件中只有1个sheet。

2文件中每个sheet有数据,需要和1文件中数据做对比,并记录是否相同。

文件内容图片:

      

import os
import xlrd as xl
from xlutils.copy import copy

def contrastFun(target_path,file_path):
    #读取原文件,也就是整理好的文件
    file = xl.open_workbook(file_path)
    #整理好的编码,全部存储入file_content_list
    file_content_list = []
    file_content_list_name = []
    file_sheet_names = file.sheet_names()
    #遍历源文件,获取全部编码
    all_row_num = 0
    for i in range(0,file.nsheets):
        sheet = file.sheet_by_index(i)
        all_row_num = all_row_num + sheet.nrows
        for j in range(sheet.nrows):
            rowValue = sheet.row_values(j,0,1)
            file_content_list.append(rowValue[0])
            file_content_list_name.append(file_sheet_names[i])
    #读取目标文件,也就是合同信息
    target = xl.open_workbook(target_path)
    saveFile = copy(target)
    sh=saveFile.get_sheet(0)
    #获取全部合同编码
    sheet = target.sheet_by_index(0)
    targetID = sheet.col_values(0)
    yes = 0
    nullRow = 0
    for i in range(0,len(targetID)):
        if sheet.cell_type(i,0) == 0:
            nullRow = nullRow+1
            continue
        for j in range(0,len(file_content_list)):
            if file_content_list[j] == targetID[i]:
                yes = yes +1
                sh.write(i,3,"*****")
                sh.write(i,4,file_content_list_name[j])
    sh.write(len(targetID),3, str(yes))
    sh.write(len(targetID),4, str(len(targetID) - yes))
    sh.write(len(targetID),5, str(nullRow))
    saveFile.save("处理结果.xls")

if __name__ == '__main__':
    #图片路径
    target_path = r'1.xls'
    #图片保存路径
    file_path = r'2.xls'
    contrastFun(target_path,file_path)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

_文和_

感觉对您有用,可以来点鼓励!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值