python excel读写与dict转换

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python excel读写与dict转换相关的知识,希望对你有一定的参考价值。

 

学习excel读写,将一个excel文件内容转换为dict用于后续处理,将dict写入excel

 

# coding = ‘utf-8‘
import time
import xlrd,xlsxwriter

start = time.clock()
def read_excel(file):
    """
    读入excel文件
    :rtype : object
    :param file:
    :return: 数据对象
    """
    try:
        data = xlrd.open_workbook(file)
        return data
    except Exception as err:
        print(err)

def write_excel(file):
    """
    读入excel文件
    :rtype : object
    :param file:
    :return: 数据对象
    """
    try:
        data = XlsxWrite.Workbook(file)
        return data
    except Exception as err:
        print(err)


def excel_to_dict(read_excel_file,excel_title):
    """
    :rtype : object
    :return dict
    """
    dict2 = {}
    data = read_excel(read_excel_file)
    table = data.sheet_by_name(ubas_info)
    for i in range(1,table.nrows):
        row_content = table.row_values(i, 0, table.ncols)
        dict1 = dict(zip(excel_title, row_content))
        dict2[i] = dict1
    return dict2


def dict_to_excel(dict_content,excel_title):
    """
    将字典写入excel中
    :type dict_content: object dict
    excel_title 列标题
    """
    dict_ing = dict_content
    excel_init_file = xlsxwriter.Workbook(out_excel_file)
    table = excel_init_file.add_worksheet(bas_info)
    title_bold = excel_init_file.add_format({bold: True, border: 2, bg_color:blue})
    border = excel_init_file.add_format({ border: 1})
    for i,j in enumerate(excel_title):
        table.set_column(i,i,len(j)+1)
        table.write_string(0,i,j,title_bold)
    for k,v in dict_content.items():
        for i in range(len(v)):
            j = v.get(excel_title[i])
            table.write_string(k,i,j,border)
    table.set_column(1,1,16)
    table.set_column(0,0,16)
  
excel_init_file.close()
if __name__ == __main__: 
  read_excel_file
= rD:\baspools_read.xlsx
  out_excel_file
= rD:\baspools_out.xlsx
  excel_title
= [basname, basip, location, baslocation, port, basprovider, portal_version, timeout, retry,auth_type]
  dict_content
= excel_to_dict(read_excel_file,excel_title) #excel to dict
  dict_to_excel(dict_content,excel_title) #dict to excel write to out_excel_file
  end
= time.clock()
  print("read: %f s" % (end - start))

 

以上是关于python excel读写与dict转换的主要内容,如果未能解决你的问题,请参考以下文章

python object与dict互相转换

pandas与JSON,HTML,EXCEL

[Python3]读写Excel - openpyxl库

python中json与dict之间转换

学习python第一次应用---编写登录接口(关于whileiffor语句,dict以及文件的读写)

python 读取excel 数据,并转为dict