# 1. 导入模块
import xlrd
from datetime import date, datetime
import xlwt
arrayNum = 6
# array = {'khbh': '', 'khjc': '', 'chmc': '', 'sl': '', 'price': '', 'jshj': ''}
# array = ['客户编号', '客户简称', '存货名称', '数量', '含税单价', '价税合计']
tables = []
newTables = []
#alist=[('11‘, ’北京市')]
alist=[]
def read_excel(filname):
# 打开文件
ll=r'/root/chat.xls'
workbook = xlrd.open_workbook(filname)
# 获取所有sheet
sheet_name = workbook.sheet_names()[0]
# 根据sheet索引或者名称获取sheet内容
sheet = workbook.sheet_by_index(0) # sheet索引从0开始
#sheet = workbook.sheet_by_name('Sheet1')
# print (workboot.sheets()[0])
# sheet的名称,行数,列数
#print(sheet.name, sheet.nrows, sheet.ncols)
# 获取整行和整列的值(数组)
rows = sheet.row_values(0) # 获取第1行内容
# cols = sheet.col_values(2) # 获取第3列内容
print(rows)
# print (cols)
for rown in range(sheet.nrows):
array = {'khbh': '', 'khjc': '', 'chmc': '', 'sl': '', 'price': '', 'jshj': ''}
#array = ['客户编号', '客户简称', '存货名称', '数量', '含税单价', '价税合计']
array['khbh'] = sheet.cell_value(rown, 6)
array['khjc'] = sheet.cell_value(rown, 1)
array['chmc'] = sheet.cell_value(rown, 2)
array['sl'] = sheet.cell_value(rown, 3)
array['price'] = sheet.cell_value(rown, 4)
array['jshj'] = sheet.cell_value(rown, 5)
tables.append(array)
print(len(tables))
# print (tables)
# print (tables[5])
if __name__ == '__main__':
#读 字典编码分类
file = open("aa.txt")
while 1:
line = file.readline().replace('n', '')
aa=line.strip().split(' ')
alist.append(aa)
if not line:
break
for i in alist:
if i ==[''] :
alist.remove(i)
#print(alist)
# 读取Excel
filename='销售发票列表21-2'
read_excel(filename+'.xls');
print('读取成功')
# print(tables)
ss=''
with open(filename+'.txt', 'w') as file_object:
for bh in alist:
#print(bh[1])
xhjshj=0
zsjshj=0
for i in tables:
if i['khbh'][:2] == bh[0]:
if i['chmc']=='A粉':
xhjshj = xhjshj+i['jshj']
if i['chmc']=='B粉':
zsjshj = zsjshj + i['jshj']
#print(bh[1], ', A粉:', xhjshj, ' , B粉:', zsjshj)
ss = filename+', '+str(bh[1])+ ', A粉 , '+str( xhjshj)+' , B粉, '+str( zsjshj)
print(ss)
# 写入Excel
file_object.write(ss)
file_object.write('n')
# print('写入成功')



