python 操作execle 进行数据字典添加列表

import openpyxl
import os
import sys

def get_max_row(sheet):
    i=sheet.max_row
    real_max_row = 0
    while i > 0:
        row_dict = {i.value for i in sheet[i]}
        if row_dict == {None}:
            i = i-1
        else:
            real_max_row = i
            break

    return real_max_row
info={}
def excel_config_info_function():
    parent = os.path.dirname(os.path.realpath(sys.argv[0]))
    #parent = os.path.dirname(os.path.realpath(__file__))--打包成exe 会路径不对
    print("获取其父目录——" + parent)  # 从当前文件路径中获取目录
    filenames_0 = os.listdir(parent+''+'begin_config')
    for f_name_0 in filenames_0:
            if f_name_0:
                wookbook = openpyxl.load_workbook(parent+''+'begin_config' + '' + f_name_0)
                book_sheet=wookbook['Sheet1']
                #清空excle配置表数据
                max_row_v=get_max_row(book_sheet)
                for x in range(1,max_row_v+1):
                    list=[]
                    for y in range(2, book_sheet.max_column + 1):
                        #去掉excel空的选项 用这个dict=={None} 过滤空字符串 excel中空字符串是None
                        dict = {book_sheet.cell(x, y).value}
                        if dict=={None}:
                            continue
                        else:
                            list.append(str(book_sheet.cell(x, y).value).strip())
                    info[str(book_sheet.cell(x, 1).value).strip()]=list
                print(info)
                print('=========ok==配置文件info执行完成========')
                return info
if __name__ ==  '__main__':
    excel_config_info_function()


python 操作execle 进行数据字典添加列表

输出结果


python 操作execle 进行数据字典添加列表

展开阅读全文

页面更新:2024-04-18

标签:数据   字符串   字典   路径   选项   操作   文件   目录   列表   科技

1 2 3 4 5

上滑加载更多 ↓
推荐阅读:
友情链接:
更多:

本站资料均由网友自行发布提供,仅用于学习交流。如有版权问题,请与我联系,QQ:4156828  

© CopyRight 2020-2024 All Rights Reserved. Powered By 71396.com 闽ICP备11008920号-4
闽公网安备35020302034903号

Top