实现文件中供应商按照供货类型的总额进行统计排序分别输出
import xlrd
data = xlrd.open_workbook(r'C:\Users\zh128\Desktop\data2.xlsx')
table = data.sheets()[0]
a = []
b = []
c = []
for i in range(1,table.ncols+1):
s=[]
temp = table.row_values(i)
s.append(temp[0])
s.append(temp[1])
sum=0
for j in range(2,len(temp)):
sum+=temp[j]
s.append(sum)
if s[1]=='A':
a.append(s)
elif s[1]=='B':
b.append(s)
else:
c.append(s)
for i in range(len(a)):
for j in range(0, len(a)-i-1):
if a[j][2] > a[j+1][2] :
a[j],a[j+1] = a[j+1], a[j]
for i in range(len(b)):
for j in range(0, len(b)-i-1):
if b[j][2] > b[j+1][2] :
b[j],b[j+1] = b[j+1], b[j]
for i in range(len(c)):
for j in range(0, len(c)-i-1):
if c[j][2] > c[j+1][2] :
c[j],c[j+1] = c[j+1], c[j]
print(a)
print("------")
print(b)
print("------")
print(c)
选取若干excel文件中的某些固定字段到一张汇总结果表
import xlrd
import xlwt
from pathlib import Path, PurePath
# 导入excel和文件操作库
# 指定要合并excel的路径
src_path = r'C:\Users\zh128\Desktop'
# 指定合并完成的路径
dst_file = r'C:\Users\zh128\结果.xlsx'
# 取得该目录下所有的xlsx格式文件
p = Path(src_path)
files = [x for x in p.iterdir() if PurePath(x).match('*.xlsx')]
# 准备一个列表存放读取结果
content = []
# 对每一个文件进行重复处理
for file in files:
# 用文件名作为每个用户的标识
username = file.stem
data = xlrd.open_workbook(file)
table = data.sheets()[0]
# 取得每一项的结果
answer1 = table.cell_value(rowx=0, colx=0)
answer2 = table.cell_value(rowx=1, colx=1)
temp = f'{
username},{
answer1},{
answer2}'
# 合并为一行先存储起来
content.append(temp.split(','))
print(temp)
# 输出
# 韩梅梅,D,B
# 李雷,D,C
# 准备写入文件的表头
table_header = ['员工姓名', '第一题', '第二题']
workbook = xlwt.Workbook(encoding='utf-8')
xlsheet = workbook.add_sheet("统计结果")
# 写入表头
row = 0
col = 0
for cell_header in table_header:
xlsheet.write(row, col, cell_header)
col += 1
# 向下移动一行
row += 1
# 取出每一行内容
for line in content:
col = 0
# 取出每个单元格内容
for cell in line:
# 写入内容
xlsheet.write(row, col, cell)
# 向右移动一个单元格
col += 1
# 向下移动一行
row += 1
# 保存最终结果
workbook.save(dst_file)
拆分文件中某些字段进行独立分离
for line in range(1,employee_number):
content = table.row_values(rowx=line, start_colx=0, end_colx=None)
# 将表头和员工数量重新组成一个新的文件
new_content = []
# 增加表头到要写入的内容中
new_content.append(salary_header)
# 增加员工工资到要写入的内容中
new_content.append(content)
# 调用自定义函数write_to_file()写入新的文件
write_to_file(filename = content[1], cnt = new_content)