1、 安装
pip install openpyxl
想要在文件中插入图片文件,需要安装pillow,安装文件:PIL-fork-1.1.7.win-amd64-py2.7.exe
· font(字体类):字号、字体颜色、下划线等
· fill(填充类):颜色等
· border(边框类):设置单元格边框
· alignment(位置类):对齐方式
· number_format(格式类):数据格式
· protection(保护类):写保护
2、写入单元格:
def init_file(self):
wb = openpyxl.Workbook()
ws1 = wb.active # 创建一个sheet
ws1["A1"] = '日期'
ws1["B1"] = "省市"
ws1["C1"] = "报价地址"
ws1["D1"] = "产品名称"
ws1["E1"] = "分类"
ws1["F1"] = "价格"
ws1["G1"] = "买/卖"
ws1["H1"] = "发布人"
ws1["I1"] = "联系方式"
ws1["J1"] = "备注"
ws.append(tit)
for d in to:
print 'AAA>>>',d
try:
ws.append(d)
wb.save('/root/nowdata.xlsx') #可重复写入
def save_execl(self,datals):
data = openpyxl.load_workbook('/root/nowdata.xlsx')
sheetnames = data.get_sheet_names()
wsobj = data.get_sheet_by_name(sheetnames[0])
wsobj.append(datals)
data.save('/root/nowdata.xlsx')
return True