首页 > 编程语言 >python 读取excel 02

python 读取excel 02

时间:2022-12-07 12:33:47浏览次数:44  
标签:02 bio name zip python excel sheetname print table

读取文件夹内的所有表,输入想要查询的表名,列出对应表页签,查询对应内容并打印出来

import xlrd
 
print('输入‘x’返回上一步!!!')
 
while True:
    print('\n输入想要查询的表:')
    bio_name = input()
    with open('dizhi.txt', 'r', encoding="utf8") as f:        # 读取txt文件中的地址
        bio_name_1 = f.read()
    bio_names = f'{bio_name_1}\\{bio_name}.xlsx'   # 将TXT中的地址与输入的名字结合,给出具体需要打开的文件
    # bio_names = f'C:\\Users\\ACE\\Desktop\\SLG\\配置表\\config\\{bio_name}.xlsx'
    b = 1
    while b > 0:
        book = xlrd.open_workbook(bio_names)  # 打开文件
        sheetname = book.sheet_names()         # 列出所有的表名
        print(sheetname)
        zip_3 = []            # 根据表名的个数,复制一个数字列表
        he = len(sheetname)+ 1
        for z in range(0,he):
            zip_3.append(str(z))
        for z_1, z_2 in zip(zip_3,sheetname):     # 根据数字列表和表名列表,依次打印出来表名的序号
            print(z_1+':', z_2)
 
        print('\n想要查询哪一个列表(输入前面的序号就好):')
        sheetname_name = int(input())
 
        # if sheetname_name > zip_3:
            # print('\n看好再输入,就没有这个序号!!!!!!!!!!!!!!!!!!!!!!!')
 
        a = 1
        while a > 0:
            print('\n请输入要搜索的词:')
            shu = input()
            book = xlrd.open_workbook(bio_names)  # 打开文件
            table = book.sheets()[sheetname_name]  # 读取第一个表格
            nrows = table.nrows     # 行数
            ncols = table.ncols     # 列数
 
 
            # 搜索关键字符串
            for element in range(nrows):
                if shu == 'x':            # 如果输入的是x,就返回上个循环
                    a = 0
                zip_1 = (table.row_values(2))
                if shu.lower() in (str(table.row_values(element))).lower():
                    zip_2 = (table.row_values(element))
                    # quit()
                    for x, y in zip(zip_1, zip_2):
                        print(x+':', y)
            else:
                print('\n该表中不存在所查找的内容!!!!')

标签:02,bio,name,zip,python,excel,sheetname,print,table
From: https://www.cnblogs.com/wang-xiao-shuai/p/16962744.html

相关文章