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 = [u'name',u'file',u'url'] 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'福尔摩斯探案集',u'http://www.fuermositanan.com/',u'推理'],'2':[u'The Sherlock Holmes stories',u'Arthur Conan Doyle',u'https://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)