听说这个功能在“达不溜皮埃斯”里面只有会员才有-python处理excel(二)

2022-07-28,,,

	女神在安徽省某个小县城的某家药房做个小财务。
	每到月初就有一堆比较枯燥的表格要处理。
	我听了她的工作内容,于是就用python来帮她处理一下。

	1.需求:根据excel中第一个sheet的某一个字段,将数据分类;
		然后分类好的数据生成多个对应的sheet。
		原表格如下(选几行参考,原表格有几万行):

想要实现的效果图如下(根据门店名称筛选出药品数据,存在一个新的sheet中):

	2.分析:选用python来操作,调用openplxy包。
	   为了保存原表的数据,应该先将原表的所有内容全部保存在字典里面;
	   然后对“药店名称”这个字段去重,放在一个新的数组中,再生成一个个的sheet,根据字符串匹配,将一条一条的数据写进对应的新sheet中;

	3,上代码:
from openpyxl import Workbook, load_workbook
from openpyxl.cell.cell import ILLEGAL_CHARACTERS_RE
import openpyxl
import re
import datetime
import sys

argv_str = sys.argv
# print(argv_str)
excel_name = "云数据 - 副本 (2).xlsx"
# excel_name = argv_str[1]
wb = load_workbook(excel_name)
sheet_names = wb.sheetnames
ws = wb[sheet_names[0]]
class item(object):
    def __init__(self):
        self.id = ''        # 编号
        self.name = ''      # 名称
        self.specs = ''     # 规格
        self.unit = ''     # 单位
        self.num = ''     # 数量
        self.price_send = ''     # 配送单价
        self.price_prime = ''     # 成本单价
        self.money_send = ''     # 配送金额
        self.money_prime = ''     # 成本金额
        self.address_name = ''    # 门店名称
row = ws.max_row
allItem_data = []
for index in range(1, row+1):
    a = item()
    a.id = ws.cell(row=index, column=1).value  # 编号
    a.name = ws.cell(row=index, column=2).value  # 名称
    a.specs = ws.cell(row=index, column=3).value  # 规格
    a.unit = ws.cell(row=index, column=4).value  # 单位
    a.num = ws.cell(row=index, column=5).value  # 数量
    a.price_send = ws.cell(row=index, column=6).value  # 配送单价
    a.price_prime = ws.cell(row=index, column=7).value  # 成本单价
    a.money_send = ws.cell(row=index, column=8).value  # 配送金额
    a.money_prime = ws.cell(row=index, column=9).value  # 成本金额
    a.address_name = ws.cell(row=index, column=18).value  # 门店名称
    allItem_data.append(a)

row_list_R = []
for cell in ws['R']:
    row_list_R.append(cell.value)
create_sheets = []
for id in row_list_R:
    if id == '门店名称':
        continue
    if id not in create_sheets:
        create_sheets.append(id)
# print(create_sheets)
# print(len(allItem_data))

sheets_obj = []
for i in range(0, len(create_sheets)):
    # sheets_obj.append(wb.create_sheet(create_sheets[i]))
    ws_index = wb.create_sheet(create_sheets[i])
    ws_index.cell(row=1, column=1).value = '门店名称'
    ws_index.cell(row=1, column=2).value = '商品编号'
    ws_index.cell(row=1, column=3).value = '商品名称'
    ws_index.cell(row=1, column=4).value = '规格'
    ws_index.cell(row=1, column=5).value = '包装单位'
    ws_index.cell(row=1, column=6).value = '数量'
    ws_index.cell(row=1, column=7).value = '配送单价'
    ws_index.cell(row=1, column=8).value = '成本单价'
    ws_index.cell(row=1, column=9).value = '配送金额'
    ws_index.cell(row=1, column=10).value = '成本金额'
    sheets_obj.append(ws_index)

print(len(allItem_data))
for i in range(0, len(allItem_data)):
    for j in range(0, len(create_sheets)):
        if create_sheets[j] == allItem_data[i].address_name:
            # 先获取每个sheet的max_row,然后再一行一行的加数据进去
            print(create_sheets[j], "=?=", allItem_data[i].address_name)
            row_max = sheets_obj[j].max_row
            sheets_obj[j].cell(row=row_max+1, column=1).value = allItem_data[i].address_name
            sheets_obj[j].cell(row=row_max + 1, column=2).value = allItem_data[i].id
            sheets_obj[j].cell(row=row_max + 1, column=3).value = allItem_data[i].name
            sheets_obj[j].cell(row=row_max + 1, column=4).value = allItem_data[i].specs
            sheets_obj[j].cell(row=row_max + 1, column=5).value = allItem_data[i].unit
            sheets_obj[j].cell(row=row_max + 1, column=6).value = allItem_data[i].num
            sheets_obj[j].cell(row=row_max + 1, column=7).value = allItem_data[i].price_send
            sheets_obj[j].cell(row=row_max + 1, column=8).value = allItem_data[i].price_prime
            sheets_obj[j].cell(row=row_max + 1, column=9).value = allItem_data[i].money_send
            sheets_obj[j].cell(row=row_max + 1, column=10).value = allItem_data[i].money_prime
            break
wb.save(excel_name)
print('完成!')
4,程序运行之后的效果图(多了好多个sheet,每个sheet中存对应的数据):

5.总结:这个程序非常简单,通过实例分享基本操作。但是对于处理大量数据还是有点耗时的,后期应该改用多线程来跑

对了,今天是1024,祝广大程序员们节日快乐!没有你们,就没有今天的智慧城市和智慧生活!

本文地址:https://blog.csdn.net/yonug1107716573/article/details/109255217

《听说这个功能在“达不溜皮埃斯”里面只有会员才有-python处理excel(二).doc》

下载本文的Word格式文档,以方便收藏与打印。