python往excel写入数据
这里提供3种python3写入数据到Excel的方式。包含2种单个sheet页的数据覆盖写入方法和1种往已有sheet页的Excel中写入新的sheet页数据的方法,可2选其一即可。
import os
import xlrd
import xlwt
import openpyxl
from xlutils.copy import copy
"""
.. note::
1.整体而言,两种包对小文件的读写速度差别不大,
而面对较大文件时,xlrd/xlwt读写速度明显优于openpyxl,
但因为xlwt无法生成xlsx是个硬伤,
所以想要尽量提高效率又不影响结果时,可以考虑用xlrd读取,用openpyxl写入
2.调用方法时数据仅支持按[]或[[],[],[]...]两种方式,其它格式的数据处理逻辑类似
"""
def write_xls_by_xlwt(trg_path, sheet_name, title, data_list):
"""
使用xlwt按单元格的方式将数据写入EXCEL文件
:param trg_path: EXCEL结果文件(eg:"D:\\data\\result.xls")
:type trg_path: string
:param sheet_name: EXCEL文件sheet页的名称(eg:"Sheet1" 或 "Sheet2"等等)
:type sheet_name: string
:param title: 表头(eg:[] 或 ["序号","用户名","密码"])
:type title: list
:param data_list: 数据(eg:["admin","123"] 或 [["admin","123456"],["root","111"]...])
:type data_list: list
.. warning::
仅支持示例中的入参格式,最终保存的EXCEL必须是.xls的,不是能是.xlsx的
可重复运行,自动覆盖原写入数据
"""
"""新建一个excel对象"""
wbk = xlwt.Workbook()
"""添加一个sheet页"""
sheet = wbk.add_sheet(sheet_name)
"""无表头时数据从第一行开始写"""
start_row = -1
"""无序号时数据从第一列开始写"""
start_col = -1
"""无序号时不写入序号"""
row_number = "false"
"""写入sheet页的表头"""
if len(title) != 0:
start_row = 0
if title[0] in ["序号", "行号", "顺序"]:
row_number = "true"
start_col = 0
for col in range(len(title)):
sheet.write(0, col, title[col])
"""循环按单元格写入sheet的每行数据"""
for row in range(len(data_list)):
"""为true时为每行数据写入序号"""
if row_number == "true":
sheet.write(row + start_row + 1, 0, row + 1)
for col in range(len(data_list)):
sheet.write(row + start_row + 1, col + start_col + 1, data_list[row][col])
"""保存excel数据文件"""
wbk.save(trg_path)
def write_xls_by_xlrd_add_sheet(trg_path, sheet_name, title, data_list):
"""
使用xlrd往一个已经存在的EXCEL中添加一个sheet页并按单元格的方式循环写入数据
:param trg_path: EXCEL结果文件(eg:"D:\\data\\result.xls")
:type trg_path: string
:param sheet_name: EXCEL文件sheet页的名称(eg:"Sheet1" 或 "Sheet2"等等)
:type sheet_name: string
:param title: 表头(eg:[] 或 ["序号","用户名","密码"])
:type title: list
:param data_list: 数据(eg:["admin","123"] 或 [["admin","123456"],["root","111"]...])
:type data_list: list
.. warning::
仅支持示例中的入参格式,最终保存的EXCEL必须是.xls的,不是能是.xlsx的
仅支持sheet_name不存在的情况
不支持openpyxl方式保存(写入)的.xls文件
"""
"""打开目标EXCEL文件"""
rb = xlrd.open_workbook(trg_path)
"""复制Excel"""
wb = copy(rb)
"""添加一个sheet页"""
sheet = wb.add_sheet(sheet_name)
"""无表头时数据从第一行开始写"""
start_row = -1
"""无序号时数据从第一列开始写"""
start_col = -1
"""无序号时不写入序号"""
row_number = "false"
"""写入sheet页的表头"""
if len(title) != 0:
start_row = 0
if title[0] in ["序号", "行号", "顺序"]:
row_number = "true"
start_col = 0
for col in range(len(title)):
sheet.write(0, col, title[col])
"""循环按单元格写入sheet的每行数据"""
for row in range(len(data_list)):
"""为true时为每行数据写入序号"""
if row_number == "true":
sheet.write(row + start_row + 1, 0, row + 1)
for col in range(len(data_list)):
sheet.write(row + start_row + 1, col + start_col + 1, data_list[row][col])
"""移除原Excel文件--上面有个copy,这里删除被copy的那个"""
os.remove(trg_path)
"""保存excel数据"""
wb.save(trg_path)
def write_xlsx_by_openpyxl(trg_path, title, data_list):
"""
使用openpyxl创建一个新的EXCEL文件并按单元格的方式循环写入数据
:param trg_path: EXCEL结果文件(eg:"D:\\data\\result.xls")
:type trg_path: string
:param title: 表头(eg:[] 或 ["序号","用户名","密码"])
:type title: list
:param data_list: 数据(eg:["admin","123"] 或 [["admin","123456"],["root","111"]...])
:type data_list: list
.. warning::
仅支持示例中的入参格式,最终保存的EXCEL可以是是.xlsx/.xls的
"""
"""创建workbook对象"""
workbook = openpyxl.Workbook()
"""激活worksheet/调用正在运行的表,默认为第一个sheet"""
sheet1 = workbook.active
"""无表头时数据从第一行开始写"""
start_row = 0
"""无序号时数据从第一列开始写"""
start_col = 0
"""无序号时不写入序号"""
row_number = "false"
"""写入sheet页的表头"""
if len(title) != 0:
start_row = 1
if title[0] in ["序号", "行号", "顺序"]:
row_number = "true"
start_col = 1
for col in range(len(title)):
sheet1.cell(row=1, column=col + 1, value=title[col])
"""循环按单元格写入sheet的每行数据"""
for row in range(len(data_list)):
"""为true时为每行数据写入序号"""
if row_number == "true":
sheet1.cell(row=row + start_row + 1, column=1, value=row + 1)
"""按单元格循环写入单行数据"""
for col in range(len(data_list)):
sheet1.cell(row=row + start_row + 1, column=col + start_col + 1, value=data_list[row][col])
"""保存excel数据"""
workbook.save(trg_path)
if __name__ == '__main__':
result_file = "C:\\Users\\result.xls"
title = ["序号", "姓名", "性别", "年龄"]
data_list = [
["张三", "男", 18],
["李四", "男", 20],
["王五", "女", 22],
["安倍", "妖", 70]
]
write_xlsx_by_openpyxl(result_file , title, data_list)
|