Python 简单读取Excel表格(xlsx文件)

昨天一个小姐姐,问我会不会Excel,作为一个单身狗,肯定不会回答我是Excel小菜鸟。

虽然不会Excel里面的函数,但是我会搜索大法,然后就看到一个openpyxl模块,小姐姐的要求很简单,就是两个Sheet中比对同样的结果。

深度截图_20180113112717.png
深度截图_20180113112758.png

写出来的代码就是下面这个样子:

#!/usr/bin/env python
# encoding: utf-8


from openpyxl import load_workbook


def mian():
    wb = load_workbook(filename='./zj.xlsx')
    ws_one = wb.get_sheet_by_name('Sheet1')
    ws_two = wb.get_sheet_by_name('Sheet2')
    result = set()

    for i in range(1,ws_one.max_row+1):
        sheet_one_order_num = ws_one.cell(row=i,column=1).value
        if sheet_one_order_num:
            for j in range(1,ws_two.max_row+1):
                for k in range(1,6):
                    sheet_two_order= ws_two.cell(row=j, column=k).value

                    if(sheet_two_order and sheet_two_order in sheet_one_order_num ):
                        log(one = i,two = j,three=k,order=sheet_one_order_num)
                        result.add(sheet_one_order_num)



    print_to_text('最后结果{}个,以下'.format(len(result)))
    print_to_text(result.__str__())


def log(one, two, three,order):
    print_to_text('第一个sheet 第{one}行 第二个sheet 第{two}行第{three}列---->{order}'.format(one=one, two=two, three=three, order=order))


def print_to_text(log):
    print(log)
    with open('result.txt','a+') as f:
        f.write(log+'\n')

if __name__ == '__main__':
    mian()

结果:


深度截图_20180113113617.png

不知道小姐姐会不会以身相许,哈哈哈哈哈哈哈

你可能感兴趣的:(Python 简单读取Excel表格(xlsx文件))