Python实现数据写入 Excel 的三种模块
导读:本文说明如何使用 xlsxwriter、pandas、openpyxl 三个库来实现数据写入 Excel。
import xlsxwriter as xw
import pandas as pd
import openpyxl as op
"-------------数据用例-------------"
orderIds = [1,2,3]
items = ['A','B','C']
myData = [10,20,30]
testData = [orderIds,items,myData]
filename1 = '测试1.xlsx'
filename2 = '测试2.xlsx'
filename3 = '测试3.xlsx'
"--------------执行----------------"
xw_toexcel(testData,filename1)
pd_toexcel(testData,filename2)
op_toexcel(testData,filename3)
01 xlsxwriter
def xw_toexcel(data,filename): # xlsxwriter库储存数据到excel
workbook = xw.Workbook(filename) # 创建工作簿
worksheet1 = workbook.add_worksheet("sheet1") # 创建子表
worksheet1.activate() # 激活表
title = ['序号','项目','数据'] # 设置表头
worksheet1.write_row('A1',title) # 从A1单元格开始写入表头
i = 2 # 从第二行开始写入数据
for j in range(len(data)):
insertData = [data[0][j],data[1][j],data[2][j]]
row = 'A' + str(i)
worksheet1.write_row(row, insertData)
i += 1
workbook.close() # 关闭表
02 pandas
def pd_toexcel(data,filename): # pandas库储存数据到excel
dfData = { # 用字典设置DataFrame所需数据
'序号':data[0],
'项目':data[1],
'数据':data[2]
}
df = pd.DataFrame(dfData) # 创建DataFrame
df.to_excel(filename,index=False) # 存表,去除原始索引列(0,1,2...)
03 openpyxl
def op_toexcel(data,filename): # openpyxl库储存数据到excel
wb = op.Workbook() # 创建工作簿对象
ws = wb['Sheet'] # 创建子表
ws.append(['序号','项目','数据']) # 添加表头
for i in range(len(data[0])):
d = data[0][i], data[1][i], data[2][i]
ws.append(d) # 每次写入一行
wb.save(filename)
小结
本文完整源码下载:
干货直达👇
评论