乐趣区

关于python:python读取excel数据转为json格式

做自动化时须要从 excel 读取数据;
本文实现将 excel 文件数据读取为 json 格局,不便自动化调用

读取 xls 文件

应用 xlrd 读取 xls 文件代码:

import xlrd


def read_xls(file):
    # 实例化 excel
    book = xlrd.open_workbook(file)
    # 下标读取 sheet
    sheet = book.sheet_by_index(1)
    # sheet name 读取 sheet
    # sheet = book.sheet_by_name('Sheet1')
    # 获取总行数
    nrows = sheet.nrows
    # 循环读取每行数据
    datas = []
    for i in range(1, nrows):
        # print(sheet.row_values(i))
        # 数据组装 dic+ t 格局
        data = dict(zip(sheet.row_values(0), sheet.row_values(i)))
        datas.append(data)
    return datas

读取 xlsx 文件

因为 xlrd 只能读取 xls,如遇到 xlsx 文件,须要应用 openpyxl 库读取,代码如下:

from openpyxl import load_workbook
def read_xlsx(file):
    # 加载文件
    book = load_workbook(file)
    # sheet name 获取 sheet:sheet = book['sheet1']
    # 获取总行数
    rows = sheet.max_row
    # 获取总列数
    # cols = sheet.max_column
    # print(rows)
    # 获取表头
    head = [row for row in sheet.iter_rows(min_row=1, max_row=1, values_only=True)][0]
    # 数据组装
    datas = []
    for row in sheet.iter_rows(min_row=2, max_row=rows + 1, values_only=True):
        data = dict(zip(head, row))
        datas.append(data)
    # print(datas)
    return datas

判断文件类型办法

最初依据文件后缀主动抉择对应办法读取

def read_excel(file: str):
    if file.endswith('xls'):
        data = read_xls(file)
    elif file.endswith('xlsx'):
        data = read_xlsx(file)
    else:
        data = ['not support file']
    return data

最终代码

import xlrd
from openpyxl import load_workbook


def read_xls(file):
    # 实例化 excel
    book = xlrd.open_workbook(file)
    # 下标读取 sheet
    sheet = book.sheet_by_index(1)
    # sheet name 读取 sheet
    # sheet = book.sheet_by_name('Sheet1')
    # 获取总行数
    nrows = sheet.nrows
    # 循环读取每行数据
    datas = []
    for i in range(1, nrows):
        # print(sheet.row_values(i))
        # 数据组装 dic+ t 格局
        data = dict(zip(sheet.row_values(0), sheet.row_values(i)))
        datas.append(data)
    return datas


def read_xlsx(file):
    # 加载文件
    book = load_workbook(file)
    # sheet name 获取 sheet:sheet = book['sheet1']
    # 获取总行数
    rows = sheet.max_row
    # 获取总列数
    # cols = sheet.max_column
    # print(rows)
    # 获取表头
    head = [row for row in sheet.iter_rows(min_row=1, max_row=1, values_only=True)][0]
    # 数据组装
    datas = []
    for row in sheet.iter_rows(min_row=2, max_row=rows + 1, values_only=True):
        data = dict(zip(head, row))
        datas.append(data)
    # print(datas)
    return datas
    # 获取单元格值:# Data = sheet.cell(row=row, column=col).value  # 获取表格内容,是从第一行第一列是从 1 开始的,留神不要丢掉 .value


def read_excel(file: str):
    if file.endswith('xls'):
        data = read_xls(file)
    elif file.endswith('xlsx'):
        data = read_xlsx(file)
    else:
        data = ['not support file']
    return data

End
流柯 https://segmentfault.com/u/cnliuke

退出移动版