tablib 导入导出excel

导出excel


def json2excel(headers, data, filename="eggs.xls"):
"""
headers = ('first_name', 'last_name')

data = [
    ('aaa', 'Adams'),
    ('George', 'Washington')
]
"""
data = tablib.Dataset(*data, headers=headers)
with open(filename, 'wb') as f:
    f.write(data.xlsx)

导入excel

xlsx格式的:


data = tablib.import_set(filepath)
data.dict

xls格式的:


f = open(filepath, 'rb')
data = tablib.import_set(f.read())
f.close()
data.dict

这些基本就能做到基本的导入导出了
具体用法:http://docs.python-tablib.org/en/latest/

你可能感兴趣的:(tablib 导入导出excel)