python 快速生成EXCEL及追加數據


 1 # -*- coding: utf-8 -*-
 2 from __future__ import unicode_literals
 3 
 4 import oss2
 5 import xlrd
 6 import xlwt
 7 from xlutils.copy import copy
 8 
 9 
10 class export_excel(object):
11     def __init__(self, p_path, rfilename, sheet_name="sheet_name", title_filed=None):
12         self.p_path = p_path  # 文件名字 "/home/xxxx/"
13         self.rfilename = rfilename  # 文件名字 "11111.xlsx"
14         self.sheet_name = sheet_name
15         self.title_filed = title_filed  # 標頭
16 
17     # 新建excel-並寫入表頭
18     def write_excel_xls(self):
19         index = len(self.title_filed)  # 獲取需要寫入數據的行數
20         workbook = xlwt.Workbook()  # 新建一個工作簿
21         sheet = workbook.add_sheet(self.sheet_name)  # 在工作簿中新建一個表格
22         for i in range(0, index):
23             for j in range(0, len(self.title_filed[i])):
24                 sheet.write(i, j, self.title_filed[i][j])  # 像表格中寫入數據(對應的行和列)
25         try:
26             workbook.save(self.p_path + self.rfilename)  # 保存工作簿
27         except Exception as e:
28             return {}
29         return {"msg": "創建excel文件成功", "code": "200"}
30 
31     # 寫入數據
32     def write_excel_xls_append(self, value):
33         index = len(value)  # 獲取需要寫入數據的行數
34         workbook = xlrd.open_workbook(self.p_path + self.rfilename)  # 打開工作簿
35         sheets = workbook.sheet_names()  # 獲取工作簿中的所有表格
36         worksheet = workbook.sheet_by_name(sheets[0])  # 獲取工作簿中所有表格中的的第一個表格
37         rows_old = worksheet.nrows  # 獲取表格中已存在的數據的行數
38         new_workbook = copy(workbook)  # 將xlrd對象拷貝轉化為xlwt對象
39         new_worksheet = new_workbook.get_sheet(0)  # 獲取轉化后工作簿中的第一個表格
40 
41         try:
42             for i in range(0, index):
43                 for j in range(0, len(value[i])):
44                     new_worksheet.write(i + rows_old, j, value[i][j])  # 追加寫入數據,注意是從i+rows_old行開始寫入
45             new_workbook.save(str(self.p_path + self.rfilename))  # 保存工作簿
46         except Exception as e:
47             return {}
48         return {"msg": "插入寫入數據成功", "code": "200"}
49 
50     # 上傳至oss-清除本地暫存文件
51     def oss_post(self):
52         subfilename = "xxxxx/" + datetime.datetime.now().strftime("%Y%m/%d/")  # oss 路徑
53         file_news = subfilename + self.rfilename
54         auth = oss2.Auth(ACCESS_KEY_ID, ACCESS_KEY_SECRET)
55         bucket = oss2.Bucket(auth, END_POINT, BUCKET_NAME)
56         with open(self.p_path + self.rfilename, "rb") as fileobj:
57             result1 = bucket.put_object(file_news, fileobj)
58         if int(result1.status) == 200:
59             return {"msg": "上傳oss成功", "code": "200", "task_url": ""}
60         else:
61             return {"msg": "上傳oss失敗", "code": "101"}
62 
63 
64 if __name__ == "__main__":
65     # 新建文件excel
66     cls_obj_export = export_excel(p_path=r'E:/GreatZhou/HD-DOC/', rfilename="1.xlsx", sheet_name="娃哈哈", title_filed=[["姓名", "性別", "年齡"], ])
67     totuch_ok = cls_obj_export.write_excel_xls()
68     if totuch_ok.get("code") == "200":
69         wr_result = cls_obj_export.write_excel_xls_append([["大頭兒子", "", "3"], ["小頭爸爸", "", "22"]])
70         print wr_result

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM