1. xlwt 安装
pip install xlwt
2. xlwt 操作, 代码
#!/usr/bin/env python3 import xlwt # 只能创建 新的 excel 文件 # 1. 创建文件对象 new_excel_file = xlwt.Workbook(encoding='utf-8') # 设置字符类型, 默认 ascii 类型 # 2. 创建表 sheet_info = new_excel_file.add_sheet("sheet_zsy_1", cell_overwrite_ok=True) # add_sheet 参数: # sheetname : 表名 # cell_overwrite_ok : 如果“真”,添加的工作表中的单元格如果写入一次以上,将不会引发异常 # -------------------------------- # 对数据进行 样式设置 # style = xlwt.XFStyle() # 初始化样式 # font = xlwt.Font() # 为样式创建字体 # font.name = 'Times New Roman' # 字体 # font.bold = True # 黑体 # font.underline = True # 下划线 # font.italic = True # 斜体字 # style.font = font # 设定样式 # # # 如果要为数据添加样式话就 # sheet_info.write(1, 0, 'Formatted value', style) # 带样式的写入 # 如果不在 write 添加 style 属性则 为默认样式 # -------------------------------- # 3. 写入数据 # 3.1. 循环写入行 for i in range(20): # 3.2. 循环获得每行数据的列表 list1 = list("abcdef") # 3.3. 循环分别插入每行的数据 for j, k in enumerate(list1): # 3.4. 向 i 行 j 列 插入 值 k sheet_info.write(i, j, k) # sheet_info.write 参数: # 参数 1 :第几行 # 参数 2 :第几列 # 参数 3 :插入的数据 # 4. 保存新文件 new_excel_file.save('adad.xls')