码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2017-08-24 15:32:34      阅读:208      评论:0      收藏:0      [点我收藏+]

标签:lib   row   books   pip   ant   one   www.   art   def   

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 多条数据 自定义写入

标签:lib   row   books   pip   ant   one   www.   art   def   

原文地址:http://www.cnblogs.com/cutesnow/p/7423226.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!