import xlwt
import xlrd
from xlutils.copy import copy
class Excel_Utils:
@staticmethod
def write_to_excel(infos,filename,sheetname='sheet1'):
work_book=xlwt.Workbook(encoding="utf-8")
sheet=work_book.add_sheet(sheetname)
head=list(infos[0].keys())
for row in range(len(head)):
sheet.write(0,row,infos[0][head[row]])
for row in range(1,len(infos)):
for col in range(len(head)):
sheet.write(row,col,infos[row][head[col]])
work_book.save(filename)
print("存储成功!!")
@staticmethod
def add_to_excel(infos,filename,sheetname='sheet1'):
work_book=xlrd.open_workbook(filename=filename)
sheet_name_list=work_book.sheet_names()
sheet=work_book.sheet_by_name(sheet_name_list[0])
old_rows=sheet.nrows
new_work_book=copy(work_book)
new_sheet=new_work_book.get_sheet(0)
head=list(infos[0].keys())
for row in range(len(infos)):
for col in range(len(head)):
new_sheet.write(row+old_rows,col,infos[row][head[col]])
new_work_book.save(filename)
print("追加成功!!!")
-
关注微信公众号【爱上开源】,该公众号会为你提供作者在网上找到有趣的开源项目,会将使用过程写成文章呈现给读者.公众号还提供爬虫和部分计算机资源给读者.如果读者想要什么资源可以私信给我,作者会尽力查询(不要涉嫌违法资源即可)