创建文件名:CreateExcel
插入列数据必须是str,多个为list[list] [[]]
插入行数据格式必须是单个str,多个为list[list] [[]]
# -*- coding=utf8 -*-
import xlsxwriter
import time
import os
class Create:
'''
Create Excel File
'''
def __init__(self, file_ads, sheet_name):
try:
if os.path.exists(file_ads):
pass
else:
os.mkdir(file_ads)
self.file_ads = file_ads + '\\' + str(time.strftime('%Y-%m-%d-%H%M%S')) + '.xlsx'
print(self.file_ads)
self.file = xlsxwriter.Workbook(self.file_ads)
self.file_sheet = self.file.add_worksheet(sheet_name)
self.bold = self.file.add_format({'bold': True}) # 创建一个加粗的格式
self.lists = [chr(i).upper() for i in range(97,123)]
except FileNotFoundError as f:
print('啊哦~出错了!', f)
print('请填写根目录或二级目录!')
def create_row(self, tatle):
self.tatle = tatle
if type(tatle) == list:
for i in range(len(tatle)):
row = self.lists[i] + str(1)
_tatle = tatle[i]
self.file_sheet.write(row, _tatle, self.bold)
else:
row = self.lists[0] + str(1)
self.file_sheet.write(row, tatle, self.bold)
def create_column(self, data):
count = 2
self.data = data
if type(data) == list:
for i in range(len(data)):
# print('data[i]=',data[i])
for s in range(len(data[i])):
_column = self.lists[s] + str(count)
# print('data[s]=',data[i][s])
self.file_sheet.write(_column, data[i][s])
count += 1
else:
_column = self.lists[0] + str(2)
self.file_sheet.write(_column, data)
self._colose()
def _colose(self):
self.file.close()
if __name__ == '__main__':
file_ads = 'F:\\lsx'
sheet_name = 'Uids'
data = 'djakdjakjd'
tatle = ['UID', 'USER_NAME']
c = Create(file_ads, sheet_name)
c.create_row(tatle)
c.create_column(data)
根据list[list]长度,便利出list中的list,
for s in range(len(data[i])):