from xls import WorkBook
from print import printdebug as p
class Data(WorkBook):
title = dict()
find_bool = True
find_result = list() # this is the result of fund
def __init__(self):
super().__init__()
def find_data(self,value):
if self.find_bool:
fr = list()
else:
fr = self.find_result #有修改,最后再传回修改后的值
lc = self.list_content #只判断不修改
n = 0
for i in range(self.cols):
for j in range(self.rows):
if lc[i][j] == value:
n += 1
fr.append([i+1,j+1])
else:
self.find_result = fr
return n
def __find_count(self,val1,val2):
self.find_bool =False
if self.find_data(val1) ==1:
if self.find_data(val2) == 1:
result = True
else:
result = False
else:
result = False
self.find_bool = True
return result
def get_title(self,val1 = '',val2 = '',val3 = ''):
#替换对象属性的方法的条件是只增加内部计算的效率
#如果要保存数据还是要存到对象里边去
title = self.title
if val1 == '':
val1 = self.val1
if val2 == '':
val2 = self.val2
if val3 == '':
val3 = self.val3
if self.__find_count(val1,val2):
fr = self.find_result
if fr[0][0] == fr[1][0]:
title['same'] = 'row'
title['row']=fr[0][0]
title[val1] = fr[0][1]
title[val2] = fr[1][1]
elif fr[0][1] == fr[1][1]:
title['same'] = 'col'
title['col']=fr[0][1]
title[val1] = fr[0][0]
title[val2] = fr[1][0]
self.title = title
print(title)
print('the '+title['same']+' is the same: '+str(title['row']))
def list_converte(self):
#原内容列表
content = self.list_content
# the finally list of content by col
list_col =list()
for i in range(self.cols):
list_col.append(list())
for j in range(self.rows):
list_col[i].append(content[j][i])
p(list_col,'this is list of col')
def get_title_dict(self):
# title 字典 key to index of col
d = dict()
if self.title['same'] == 'row':
list_key = self.list_content[self.title['row']-1]
p(list_key,'listkey')
for i in range(self.cols):
d[list_key[i]] = i
p(d,'this is title dict')
val1 = 'Total'
val2 = 'Total+directive'
val3 = 'name'
#xlapp.open_new(path)
'''
第一步,获得文件名 path 创建excel对象 xlapp = WorkBook()
每二步,打开文件 xlapp.get_workbooks(path) 这里已经有了 xlBook xlbk_name filename rows cols
第三步,执行获得使用区域 xlapp.get_usedrange()) 返回的是区域 ,保存的是 区域内的数据 list_content
第四步,确定标题栏的位置,是同行还是同列,查找类型,字符串(分别代表数据,和数据所对应的关键字)
第五步,读取数据list_to_data,确定数据位置,依据关键字,区分方法
第六步,锁定,目标工作表位置,插入数据位置,byrows,or,bycolumn,依据是关键字
第七步,设置数据的值data_to_list
第八步,要实现行列转换的思想,还要确定匹配度的问题
'''
# step 1
path = 'C:\\Users\\wangcc\\Desktop\\工作簿1.csv'
xlapp = Data()
# step 2
xlapp.get_workbooks(path)
# step 3
xlapp.get_usedrange()
# step 4
xlapp.get_title(val1,val2,val3)
# step 5
xlapp.list_converte()
xlapp.get_title_dict()
excel-xls-data_handling
最新推荐文章于 2023-08-23 11:00:50 发布