使用xlsxwriter 將內容寫入Excel中
首先應該安裝xlsxwirter
方法:pip install xlsxwirter
寫入結果#!/usr/bin/env python
# -*- coding:utf-8 -*-
import xlsxwriter
import time
'''
#登錄結果寫入excel文件中
class Xlloginfo(object):
def __init__(self,path=''):
#文件名稱fname
fname = path +time.strftime('%Y-%m-%d %H %M %S')
#第一行
self.row = 0
#創建表格文件
self.xl = xlsxwriter.Workbook(path+fname+'.xlsx')
#表格背景色爲紅色
self.style = self.xl.add_format({'bg_color':'red'})
def xl_write(self,*args):
#第一列
col = 0
style = ''
if 'error' in args:
style = self.style
for val in args:
#在一行中寫入數據
self.sheet.write_string(self.row,col,val,style)
col +=1
self.row +=1
def log_init(self,sheetname,*title):
#新增表單sheetname
self.sheet = self.xl.add_worksheet(sheetname)
#設定列寬
self.sheet.set_column('A:E',30)
self.xl_write(*title)
def log_write(self,*args):
self.xl_write(*args)
def log_close(self):
self.xl.close()
x=Xlloginfo()
d=["1","2","3","3"]
x.log_init("sss","ssq","www","wwwd")
x.xl_write(d)
x.log_close()