|
根据到货记录表中的物料信息,按物料编码去重,求和在本表汇总表中得出物料编码,名称,规格,厂商,单位,到货数量的汇总到本表中。
用数组+字典按在到货记录中按物料号去重汇总求和到汇总表中。
# @Version: 1.0
# @Author : 汤灏
# @File : delivery_aggregate1.0.py
# @Time : 2024/2/2 11:21
from functools import reduce
import numpy as np
import openpyxl
import pandas as pd
from openpyxl.utils.dataframe import dataframe_to_rows
df = pd.read_excel('到货模板.xlsx', sheet_name='到货记录')
# print(df)
temp_df = (
df.groupby(['物料编码', '物料名称', '厂商', '规格型号/图号', '单位','货位'], as_index=False).agg(
到货数量汇总=('到货数量', 'sum'))
)
# temp_df.insert(5, '需求数量', None)
df_demand = pd.read_excel('到货模板.xlsx', sheet_name='BOM清单')
df_demand_agg=(
df_demand.groupby('物料编码',as_index=False).agg(需求汇总=('需求数量','sum'))
)
# print(df_demand_agg)
df_dispatch=pd.read_excel('到货模板.xlsx',sheet_name='发出记录')
df_dispatch_agg=(
df_dispatch.groupby('物料编码',as_index=False).agg(发出总数=('发出数量','sum'))
)
# print(df_dispatch_agg)
df_merged = reduce(lambda left, right: pd.merge(left, right, on=['物料编码'],how='outer'), [temp_df,df_dispatch_agg,df_demand_agg])
print(df_merged)
df_merged=df_merged.assign(欠料数量=df_merged['到货数量汇总'].sub(df_merged['需求汇总'],fill_value=0),
库存数量=df_merged['到货数量汇总'].sub(df_merged['发出总数'],fill_value=0),
预警=np.where(df_merged['发出总数']>df_merged['需求汇总'],'预警', None)
)
print(df_merged)
print(df_merged.columns)
col=['物料编码', '物料名称', '厂商', '规格型号/图号', '单位', '需求汇总', '到货数量汇总', '欠料数量', '发出总数', '库存数量', '预警', '所用机型', '货位']
df_merged=df_merged.reindex(columns=col)
print(df_merged)
wb = openpyxl.load_workbook('到货模板.xlsx')
ws = wb['汇总']
for idx, rows in enumerate(dataframe_to_rows(df_merged, header=False, index=False), start=4):
for col_idx, cell in enumerate(rows, start=2):
ws.cell(idx, col_idx, value=cell)
wb.save('zhuan1.xlsx') |
评分
-
1
查看全部评分
-
|