好的,鱼油,让我们一起愉快地完成这个任务吧!以下是每一步的通俗易懂且幽默的解答:
1. **初始化相关变量和对象**:
import openpyxl
wb = openpyxl.load_workbook('你的工作簿.xlsx')
sheet1001 = wb['sheet1001']
_初始化好像做饭前要准备食材,把锅碗瓢盆都弄出来。_
2. **检查并处理“厚度汇总”和“客户汇总”工作表,如果不存在则新建并设置表头**:
if '厚度汇总' not in wb.sheetnames:
wb.create_sheet('厚度汇总')
if '客户汇总' not in wb.sheetnames:
wb.create_sheet('客户汇总')
# 设定表头
thickness_summary = wb['厚度汇总']
customer_summary = wb['客户汇总']
thickness_summary.append(['厚度', '平方总和', '立方总和', '总金额'])
customer_summary.append(['客户名称', '厚度', '平方总和', '立方总和', '总金额'])
_这就像在家找衣服,发现没有就买新的,还得给新衣服贴上标签。_
3. **从“sheet1001”工作表中提取数据,根据 E 列是否为空进行筛选,只选取E列不为空**:
valid_rows = [row for row in sheet1001.iter_rows(min_row=2) if row[4].value]
_挑选数据就像在超市买菜,只选新鲜的。_
4. **“sheet1001”sheet的A列,汇总到“客户汇总”sheet的“客户名称”列**:
5. **“sheet1001”sheet的E列,汇总到“客户汇总”sheet的“厚度”列**:
6. **“sheet1001”sheet的D列,汇总到“客户汇总”sheet的“平方总和”列**:
7. **“sheet1001”sheet的F列,汇总到“客户汇总”sheet的“立方总和”列**:
8. **“sheet1001”sheet的J列,汇总到“客户汇总”sheet的“总金额”列**:
for row in valid_rows:
customer_summary.append([row[0].value, row[4].value, row[3].value, row[5].value, row[9].value])
9. **“sheet1001”sheet的E列,汇总到“厚度汇总”sheet的“厚度”列**:
10. **“sheet1001”sheet的D列,汇总到“厚度汇总”sheet的“平方总和”列**:
11. **“sheet1001”sheet的F列,汇总到“厚度汇总”sheet的“立方总和”列**:
12. **“sheet1001”sheet的J列,汇总到“厚度汇总”sheet的“总金额”列**:
for row in valid_rows:
thickness_summary.append([row[4].value, row[3].value, row[5].value, row[9].value])
13. **检查一下厚度汇总数据跟客户汇总数据,跟“sheet1001”工作表中相应数据是否对应,不对的重新进行1-12步骤**:
_这里需要一个函数来验证并重复上述步骤,假如有问题,可以循环执行或者抛出异常。_
def validate_and_repeat():
# 假设实现了验证逻辑,这里简化成简单的比较
for row in valid_rows:
# 简单的对比逻辑
if not all([row[0].value in [r[0].value for r in customer_summary],
row[4].value in [r[1].value for r in customer_summary],
row[3].value in [r[2].value for r in customer_summary],
row[5].value in [r[3].value for r in customer_summary],
row[9].value in [r[4].value for r in customer_summary]]):
print("有问题,重新来过!")
main() # 重新执行主要步骤
break
validate_and_repeat()
14. **对于“厚度汇总”的数据,所有厚度相同的平方数、立方数、总金额进行汇总计算**:
from collections import defaultdict
thickness_data = defaultdict(lambda: [0, 0, 0]) # 厚度:[平方总和,立方总和,总金额]
for row in thickness_summary.iter_rows(min_row=2):
thickness = row[0].value
thickness_data[thickness][0] += row[1].value
thickness_data[thickness][1] += row[2].value
thickness_data[thickness][2] += row[3].value
thickness_summary.delete_rows(2, thickness_summary.max_row)
for thickness, data in thickness_data.items():
thickness_summary.append([thickness] + data)
15. **对于“客户汇总”的数据,所有客户名称相同的行,统一放一起**:
16. **对“客户汇总”数据,先把客户名称按拼音排个序,保证同一客户的数据集中在一起**:
17. **对“客户汇总”数据,把同一客户的数据,按厚度排个序**:
18. **把“客户汇总”数据,同一客户的,且厚度相同的,汇总一下**:
from pypinyin import lazy_pinyin # 需要额外安装 pypinyin 库
customer_data = defaultdict(lambda: defaultdict(lambda: [0, 0, 0])) # 客户名称:[厚度:平方总和,立方总和,总金额]
for row in customer_summary.iter_rows(min_row=2):
customer_name = row[0].value
thickness = row[1].value
customer_data[customer_name][thickness][0] += row[2].value
customer_data[customer_name][thickness][1] += row[3].value
customer_data[customer_name][thickness][2] += row[4].value
customer_summary.delete_rows(2, customer_summary.max_row)
sorted_customer_names = sorted(customer_data.keys(), key=lambda name: ''.join(lazy_pinyin(name)))
for customer_name in sorted_customer_names:
sorted_thickness = sorted(customer_data[customer_name].items())
for thickness, data in sorted_thickness:
customer_summary.append([customer_name, thickness] + data)
19. **清除“厚度汇总”和“客户汇总”工作表中的空白行**:
def remove_blank_rows(sheet):
for row in sheet.iter_rows():
if all(cell.value is None for cell in row):
sheet.delete_rows(row[0].row)
remove_blank_rows(thickness_summary)
remove_blank_rows(customer_summary)
20. **自动调整“厚度汇总”和“客户汇总”工作表的列宽**:
def adjust_column_width(sheet):
for col in sheet.columns:
max_length = 0
column = col[0].column_letter
for cell in col:
try:
if len(str(cell.value)) > max_length:
max_length = len(str(cell.value))
except:
pass
adjusted_width = (max_length + 2)
sheet.column_dimensions[column].width = adjusted_width
adjust_column_width(thickness_summary)
adjust_column_width(customer_summary)
最后,记得保存你的工作:
这就完成了所有的需求步骤。希望这些代码片段和解释让你感觉像是在和老朋友聊天,一边喝茶一边编程!如果有任何问题或需要进一步的帮助,请随时提问!
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。 |