# -*- coding:utf-8 _*-

"""

@author:Administrator

@file: excel.py

Description :

如果行数是1000的倍数,进行一次flush,如果行数超过65536,

新开一个sheet,如果超过3个sheet,则新建一个文件

@time: 2018/10/31

"""

import os

import xlwt

from xlrd import open_workbook

from xlutils.copy import copy

from configs.config import ConfigENum

MAX_ROW_NUM = 65536

MAX_SHEET_NUM = 3

class XLS:

def __init__(self, name, captionlist: list, typelist: list, encoding='utf8', flushBound=1000):

"""

:type captionlist: list

:type typelist: list

"""

self.name = name

self.captionlist = captionlist[:]

self.typeList = typelist[:]

self.encoding = encoding

self.flushBound = flushBound

self.bk = xlwt.Workbook(encoding=self.encoding, style_compression=0)

self.workbookIndex = 1

self.row = 0

self.excel_name = None

self.sheet = None

self.sheetindex = 0

# self._add_sheet()

def _add_sheet(self):

if self.sheetindex != 0:

# This method is used to save the Workbook to a file in native Excel format.

self.bk.save(self.name + str(self.sheetindex) + ".xls")

# create new workbook

if self.sheetindex > MAX_SHEET_NUM:

self.workbookIndex += 1

self.bk = xlwt.Workbook(encoding=self.encoding, style_compression=0)

self.sheetindex = 1

# a new sheet

index=self.sheetindex

print("self.sheet_index=",index)

self.sheet = self.bk.add_sheet(self.name +

index.__str__(),

cell_overwrite_ok=False)

for i in range(len(self.captionlist)):

# This method is used to write a cell to a :class:`Worksheet`

self.sheet.write(0, i, self.captionlist[i])

self.row = 1

def write(self, data: list):

"""

:type data: list

"""

# The row of current sheet > the max rows of sheet then create a new sheet

if self.row > MAX_ROW_NUM:

self.sheetindex += 1

self._add_sheet()

self._add_sheet()

for i in data:

for j in range(len(i)):

if self.typeList[j] == "num":

try:

self.sheet.write(self.row, j, float(i[j]))

except:

raise ValueError("{} is not a number".format(i[j]))

else:

self.sheet.write(self.row, j, i[j])

# when rows =1000 then flush rows

if self.row % self.flushBound == 0:

self.sheet.flush_row_data()

self.row += 1

@staticmethod

def __find_file(keyword):

for root, d, files in os.walk(ConfigENum.DATA_PATH.value):

for file in files:

if keyword in file:

file = os.path.join(root, file)

return file

return False

def add_write(self, datas):

result=self.__find_file(self.name)

if not result:

self.write(datas)

self.save()

return "ok"

if self.row > MAX_ROW_NUM:

self.sheetindex += 1

self._add_sheet()

# read a excel file

self.bk = open_workbook(result)

# get the rows of sheet

rows = self.bk.sheets()[self.sheetindex].nrows

# Copy an :class:`xlrd.Book` into an :class:`xlwt.Workbook`

excel = copy(self.bk)

self.sheet = excel.get_sheet(self.sheetindex)

self.row = rows

for data in datas:

# [1,2,3,4]

for j in range(len(data)):

self.sheet.write(self.row, j, data[j]) # xlwt对象的写方法,参数分别是行、列、值

if self.row % self.flushBound == 0:

self.sheet.flush_row_data()

self.row += 1

self.excel_name = os.path.join(ConfigENum.DATA_PATH.value,

self.name + self.workbookIndex.__str__() + ".xls")

excel.save(self.excel_name)

def save(self):

self.excel_name = os.path.join(ConfigENum.DATA_PATH.value,

self.name + self.workbookIndex.__str__() + ".xls")

self.bk.save(self.excel_name)

# if __name__ == "__main__":

# caption_list = ["name", "gender", "age"]

# type_list = ["str", "str", "num"]

# input_data = ["Lili", "M", 25]

# excl=XLS("test",caption_list,type_list)

# excl.write(input_data)

# excl.save()

python excel 追加写入_python excel写入及追加写入相关推荐

  1. python excel生成图表_python excel 之 按格式生成图表和数据

    python excel 针对python 对excel的操作目前有已经有很多库可以使用, 比如最常用的读写库xlrd,xlwt,xlutils 对xlsx.xlsm读写的openpyxl 针对xls ...

  2. python做excel表格柱状图_Python Excel 绘制柱形图

    原博文 2019-11-19 22:07 − 本文主要讲述如何使用Python操作Excel绘制柱形图. 相关代码请参考 https://github.com/RustFisher/python-pl ...

  3. python excel详解_Python - excel 详解

    Python读excel,2003用xlrd,2007和2010用openpyxl xlrd介绍:http://pypi.python.org/pypi/xlrd 转自:http://huaxia52 ...

  4. python体能达标成绩_Python+Excel数据分析实战:军事体能考核成绩评定(九)评定个人等级...

    这一章我们实现个人成绩的等级评定,根据单杠.仰卧起坐.蛇形跑.3公里跑成绩以及体型,综合评定个人等级. 相关规定:体型合格就不用考虑单项不合格的情况,只看通用训练科目成绩总分(单杠.仰卧起坐.蛇形跑. ...

  5. spss与python和sql区别_Python/Excel/SPSS/SQL数据处理方法比较之2 - 数据查看

    继续这个系列.我们导入了数据,接下来做一下基本的查看. Python 我们的处理对象依然是DataFrame对象df. 首先使用head()函数(或tail()函数)查看最前(最后)的5条记录,获取粗 ...

  6. python查找excel中内容_python excel表格数据-python 如何读取 excel 指定单元格内容

    python 怎么从excel中读取数据 VLOOKUP是函数,给定一个查找的目标,它就能从指定的查找中查找返回想找到的值.它的基本语法为: VLOOKUP(查找目标,查找范围,返回值的列数,精确OR ...

  7. python 拆分excel单元格_Python Excel 单元格 拆分并填充内容,功能和的wps类似

    一.实现的功能: 二.效果 1.拆分并填充前 2.拆分并填充后 三.代码 import openpyxl from copy import deepcopy # 原文:https://www.cnbl ...

  8. python关于excel格式刷_python excel 格式刷_今天竟然发现了Excel 的明显bug,格式刷一定要小心用...

    没想到,象excel这样的成熟软件,竟然能被我发现bug,但是这个发现浪费了我整整一个下午,不过好在还发现了.否则,求和总额不对,后果不堪设想. 中国人制表的习惯和老外是很有些不同的,就像老外基本上是 ...

  9. python 按行写入_Python中将变量按行写入txt文本

    {"moduleinfo":{"card_count":[{"count_phone":1,"count":1}],&q ...

最新文章

  1. 2022-2028年中国应急救援装备行业市场研究及前瞻分析报告
  2. ORA-01102: cannot mount database in EXCLUSIVE mode
  3. 《C++入门经典(第6版)》——1.5 问与答
  4. 轻量社交APP系统ThinkSNS 简 权威发布 限时惠购
  5. 进程调度算法 C++实现
  6. CF932G-Palindrome Partition【PAM】
  7. 写Rap,编菜谱,你画我猜……这些 AI demo 我可以玩一天!
  8. 五种编程语言解释数据结构与算法——顺序表3(JavaScript与Python语言实现)
  9. 四级英语图表作文真题计算机,四级作文辅导二:图表类作文(范文、模板、必备句型。对了,还有作业)...
  10. 7名高管、半数员工离职,如何再造乔布斯重返苹果神话?
  11. JAVA入门到精通-第16讲-数组
  12. js制作带有遮罩弹出层实现登录小窗口
  13. Spring 基于注解的AOP实现
  14. Repeater、GirdView、DataList通用的分页代码
  15. buf.readInt8函数详解
  16. Excel常用函数——count
  17. WinRar去除广告弹窗
  18. 非接触物体尺寸形态测量(G 题)
  19. android exo解码问题,android – exoplayer-自动更改质量不起作用(hls)
  20. Java培训机构哪个好?该怎么选择

热门文章

  1. Mangofile.PersonalPlus5(x86) Crack
  2. iOS_多线程(一)
  3. 人是要有一点精神的!
  4. 推荐系列:2008年第07期 总9期
  5. 利用永恒之蓝入侵服务器复制文件,永恒之蓝漏洞利用复现(示例代码)
  6. mybatis Android,mybatis使用selectByPrimaryKey出错
  7. dw6能编译asp吗,让Adobe Dreamweaver CC支持ASP
  8. python内置函数sorted(x)的作用是_Python内置filter与sorted函数
  9. java局部变量说法不正确的是_关于Java的成员变量和局部变量,下面说法错误的是...
  10. python 字符串可以直接连接吗_如何连接字符串。。。在?(Python)