# -*- coding: utf-8 -*-
from xlwt import *
import os
import oss2
from xlrd import open_workbook
from xlutils.copy import copy
import datetime
import random
import sys
import traceback
PREFIX = 'http://'
class cexport_excel():
"""excel追加寫入"""
# excheader表頭
# datasrcset 二維元祖
# path = 'hh.xls'
def export_excel(self,path,excname,excheader,datasrcset,username, ossinfo,urlload):
result = {}
try:
# 寫頭
if not os.path.exists(path):
w = Workbook()
sheet = w.add_sheet(excname)
headercol = 0
for header in excheader:
sheet.write(0, headercol, str(header).decode('utf-8'))
headercol += 1
w.save(path)
# 追加寫入excel
rexcel = open_workbook(path)
rows = rexcel.sheets()[0].nrows
excel = copy(rexcel)
table = excel.get_sheet(0)
newrow = rows
for i, row in enumerate(datasrcset):
for j, col in enumerate(row):
table.write(newrow, j, col) # datasrcset
newrow += 1
excel.save(path)
osspath = username + '_' + datetime.now().strftime('%Y%m%d%H%M%S') + '_' + str(random.randint(0, 99)) + '.xls'
auth = oss2.Auth(ossinfo['ACCESS_KEY_ID'], ossinfo['ACCESS_KEY_SECRET'])
bucket = oss2.Bucket(auth, ossinfo['ENDPOINT_OUT'], ossinfo['BUCKETNAME_XLS'])
bucket.put_object('%s/%s' % (urlload, osspath), open(path))
result['osspath'] = PREFIX + ossinfo['BUCKETNAME_XLS'] + '.' + ossinfo['ENDPOINT_OUT'] + '/' + urlload + '/' + osspath
result['errorcode'] = 0
return result
except Exception as ex:
result['errorcode'] = -1
result['errortext'] = '%s:Exception = %s ex=%s __LINE__=%s' % (traceback.print_exc(), Exception, ex, sys._getframe().f_lineno)
return result
python 追加寫入到excel
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.