import pandas as pd
from openpyxl import load_workbook
def append_df_to_excel(filename, df, sheet_name='Sheet1', startrow=None,
truncate_sheet=False,
**to_excel_kwargs):
"""
Append a DataFrame [df] to existing Excel file [filename]
into [sheet_name] Sheet.
If [filename] doesn't exist, then this function will create it.
Parameters:
filename : File path or existing ExcelWriter
(Example: '/path/to/file.xlsx')
df : dataframe to save to workbook
sheet_name : Name of sheet which will contain DataFrame.
(default: 'Sheet1')
startrow : upper left cell row to dump data frame.
Per default (startrow=None) calculate the last row
in the existing DF and write to the next row...
truncate_sheet : truncate (remove and recreate) [sheet_name]
before writing DataFrame to Excel file
to_excel_kwargs : arguments which will be passed to `DataFrame.to_excel()`
[can be dictionary]
Returns: None
"""
# from openpyxl import load_workbook
# import pandas as pd
# ignore [engine] parameter if it was passed
if 'engine' in to_excel_kwargs:
to_excel_kwargs.pop('engine')
writer = pd.ExcelWriter(filename, engine='openpyxl')
# Python 2.x: define [FileNotFoundError] exception if it doesn't exist
try:
FileNotFoundError
except NameError:
FileNotFoundError = IOError
try:
# try to open an existing workbook
writer.book = load_workbook(filename)
# get the last row in the existing Excel sheet
# if it was not specified explicitly
if startrow is None and sheet_name in writer.book.sheetnames:
startrow = writer.book[sheet_name].max_row
# truncate sheet
if truncate_sheet and sheet_name in writer.book.sheetnames:
# index of [sheet_name] sheet
idx = writer.book.sheetnames.index(sheet_name)
# remove [sheet_name]
writer.book.remove(writer.book.worksheets[idx])
# create an empty sheet [sheet_name] using old index
writer.book.create_sheet(sheet_name, idx)
# copy existing sheets
writer.sheets = {ws.title: ws for ws in writer.book.worksheets}
except FileNotFoundError:
# file does not exist yet, we will create it
pass
if startrow is None:
startrow = 0
# write out the new sheet
df.to_excel(writer, sheet_name, startrow=startrow, **to_excel_kwargs)
# save the workbook
writer.save()
print('---------------脚本开始')
df = pd.read_excel('10.18 HPC.xlsx',index_col=None,usecols=['供商编码','门店','商品','当前库存'])
df1 = df[df['当前库存'] < 1]
零库存总数 = df1.value_counts('供商编码')
c = 零库存总数.index.values
d = 零库存总数.values
print(零库存总数) #供商编码对应0库存总数
单品总数 = df.value_counts('供商编码')
a = 单品总数.index.values
b = 单品总数.values
print('---------------')
#供商编码和库存总数
df3 = pd.DataFrame({'供商编码':a,'库存总数':b})
print(df3)
#append_df_to_excel('1.xlsx', df3, sheet_name='Sheet1', startcol=0,startrow=0,index=False,header=1,)
#零库存总数
df4 = pd.DataFrame({'供商编码':c,'零库存总数':d})
print(df4)
#append_df_to_excel('1.xlsx', df4, sheet_name='Sheet1', startcol=2,startrow=0,index=False,header=1,)
print('---------------脚本结束')
#vlookup
r = pd.merge(df3,
df4,
how = 'left',
on = '供商编码',
)
print(r)
#写入库存总数和零库存总数
append_df_to_excel('1.xlsx',
r,
sheet_name='Sheet1',
startcol=0,
startrow=0,
index=False,
header=1)
作业-自动生成零库存总数和库存总数
最新推荐文章于 2024-04-01 10:30:00 发布