Python生成excel表格并设置样式
python在做爬虫时会涉及到数据存储问题,下面说一下将数据存储在excel表格中,主要使用扩展类xlwt,下面详细说一下,主要涉及到了数据列,背景,名称等参数。
说明:python3.7、window环境
一、类库安装
pip install xlwt
二、示例代码
# -*- coding: utf-8 -*- import xlwt import time # 生成表格文件 def create_file(content): # 初始化样式 style_head = xlwt.XFStyle() # 初始化字体相关 font = xlwt.Font() font.name = "微软雅黑" font.bold = True # 必须是数字索引 font.colour_index = 1 # 初始背景图案 bg = xlwt.Pattern() # May be: NO_PATTERN, SOLID_PATTERN, or 0x00 through 0x12 bg.pattern = xlwt.Pattern.SOLID_PATTERN # May be: 8 through 63. 0 = Black, 1 = White, 2 = Red, 3 = Green, 4 = Blue, 5 = Yellow, 6 = Magenta, 7 = Cyan, 16 = Maroon, 17 = Dark Green, 18 = Dark Blue, 19 = Dark Yellow , almost brown), 20 = Dark Magenta, 21 = Teal, 22 = Light Gray, 23 = Dark Gray bg.pattern_fore_colour = 4 # 设置字体 style_head.font = font # 设置背景 style_head.pattern = bg # 创建一个excel excel = xlwt.Workbook(encoding='utf-8') # 添加工作区 sheet = excel.add_sheet("演示表格") # xlwt中是行和列都是从0开始计算的 first_col_1 = sheet.col(1) first_col_3 = sheet.col(3) # 设置创建时间宽度 first_col_1.width = 256 * 15 # 设置存储路径列宽度 first_col_3.width = 256 * 100 # 标题信息 head = ["序号", "插入时间", "标题", "英文名称"] for index, value in enumerate(head): sheet.write(0, index, value, style_head) # 循环写入 for index, value_list in enumerate(content, 1): for i, value in enumerate(value_list): sheet.write(index, i, value) # 保存excel file_name = time.time() excel.save("./%s.xls" % file_name) return file_name if __name__ == '__main__': data_list = [ (0, 1580719141, '视频|有你,才有胜利!', 'You are the winner!'), (1, 1580719142, '中央军委批准!军队承担武汉生活物资配送供应任务', "Approved by the Central Military Commission! The military undertakes the task of distribution and supply of living materials in Wuhan") ] data = create_file(data_list) print("success") print(data)
三、导出效果
四、注意事项
1、开发过程中遇到了生成文件名后缀为xlsx格式,无法打开,故设置为xls格式。
2、设置列宽时需要注意,xlwt是从0开始计数的。