zoukankan      html  css  js  c++  java
  • 使用python读写操作excel内两个不同表(基于python3.6)

    #!/usr/bin/python3
    # -*- coding: utf-8 -*-
    # @File    : csvHandle
    # @Author  : moucong
    # @Date    : 2019/3/14 15:28
    # @Software: PyCharm

    import openpyxl


    def csv_handle():
        filename = "all.xlsx"
        wb = openpyxl.load_workbook(filename)
        sheets = wb.sheetnames    #拿到表名
        sheet_prize = wb[sheets[0]]  #拿到组内的表名
        sheet_visitor = wb[sheets[1]]
        for r in range(1, sheet_prize.max_row + 1):
            prize_value = str(sheet_prize.cell(row=r, column=2).value)
            # print(str(sheet_prize.cell(row=r, column=2).value))
            for i in range(1, sheet_visitor.max_row + 1):
                vistor_value = sheet_visitor.cell(row=i, column=1).value
                if vistor_value == prize_value:
                    print(str(vistor_value) + "匹配成功!")
                    mobile = str(sheet_visitor.cell(row=i, column=19).value)   #可将某值调出查看,row'行',column'列'
                    company = str(sheet_visitor.cell(row=i, column=6).value)
                    jobTitle = str(sheet_visitor.cell(row=i, column=7).value)
                    line_company = 'I' + str(r)
                    line_mobile = 'J' + str(r)
                    line_jobTitle = 'K' + str(r)
                    sheet_prize[line_mobile] = mobile       #直接赋值给表中的J列r行
                    sheet_prize[line_company] = company
                    sheet_prize[line_jobTitle] = jobTitle
                    print(sheet_prize[line_mobile].value)
                    break
                else:
                    print(str(prize_value) + '' + "没有该用户 ")
                    continue
        print('完成匹配,正在保存!')
        wb.save(filename=filename)   #记住一定要保存


    if __name__ == '__main__':
        csv_handle()

  • 相关阅读:
    cmder
    navicat 查询保存的位置
    git使用
    怎么保证测试用例的覆盖率
    python3.7-初学篇-19-良好的习惯
    python基础篇-使用list和tuple
    python基础篇-字符串和编码
    python基础篇-输入和输出
    python3.7-初学篇-21
    python3.7-初学者-20
  • 原文地址:https://www.cnblogs.com/setname/p/10531630.html
Copyright © 2011-2022 走看看