Python Excel 多sheet 多条数据 自定义写入

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python Excel 多sheet 多条数据 自定义写入相关的知识,希望对你有一定的参考价值。

pip install xlwt

python excel 数据写入操作,处理网站数据导出以及不是太多数据的爬虫存储, 用处蛮多的轮子。 

(150+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++=)

 1 #coding: utf-8
 2 import xlwt
 3 from xlwt import Workbook
 4 
 5 
 6 wb = Workbook()
 7 ws = wb.add_sheet(Product)
 8 ws_1 = wb.add_sheet(Other)
 9 
10 fontSize = xlwt.easyxf(font:height 200, name Calibri; align: horiz center;)   #字体自定义
11 
12 row0 = [u名目,u链接,u备注]
13 row1 = [uname,ufile,uurl]
14 
15 def title_write(row,ws):
16     #生成标题
17     for i in range(0,len(row)):
18             col = ws.col(i)
19             col.width=256*18
20             if i == (len(row)-1):
21                 ws.col(i).width = 256*23
22             ws.write(0,i,row[i],xlwt.easyxf(font:height 200, name Arial_Unicode_MS, colour_index black, bold on;align: horiz center;))
23 
24 title_write(row0,ws)
25 title_write(row1,ws_1)
26 
27 data = [{1:[u福尔摩斯探案集,uhttp://www.fuermositanan.com/,u推理],2:[uThe Sherlock Holmes stories,uArthur Conan Doyle,uhttps://ebooks.adelaide.edu.au/d/doyle/arthur_conan/]}]#这里演示写死了,根据实际需求更改
28 
29 x = 1
30 for i in data:
31     rows_0 = i[1]
32     rows_1 = i[2]
33     def rows_write(content,ws):
34         #生成内容
35         for num,rows in enumerate(content):
36             rows if rows != None else ‘‘
37             ws.write(x, num, rows, fontSize)
38     rows_write(rows_0,ws)
39     rows_write(rows_1,ws_1)
40     x += 1
41 
42 fname=Books.xls
43 wb.save(fname)

 

以上是关于Python Excel 多sheet 多条数据 自定义写入的主要内容,如果未能解决你的问题,请参考以下文章

如何在excel里面进行一对多的模糊匹配

python 获取excel文件的所有sheet名字

超级简单POI多sheet导出Excel实战

excel散点图:如何用多组数据拟合一条曲线

Python读取excel数据

pandas写入多组数据到excel不同的sheet