python代码: 1. 使用excel进行文档管理,A列序号,B列项目名称,C列供应商名称,D列需求总条数,E列FC条数,F列NC条数 2. 可以添加供应商,修改某个供应商的FC条数,NC条数 3. 可以删除某个供应商的数据 4. 可以对同一个文档根据FC条数进行排序 5. 可以对同一个文档根据NC条数进行排序 6. 可以查看某个供应商的数据 7. 需要有输入合法性校验 8. 需要根据C, E列生成数据透视图到新的sheet页 9. 需要根据C, F列生成数据透视图到新的sheet页 10. 需要根据C, E列生成折线图到新的sheet页 11. 需要根据C, F列生成折线图到新的sheet页
时间: 2024-03-25 12:40:05 浏览: 118
pandas.DataFrame的pivot()和unstack()实现行转列
这是一个比较复杂的需求,需要使用Python的pandas和openpyxl库来实现。以下是一个初步的代码框架,你可以根据自己的需求进行修改和完善。
```
import pandas as pd
from openpyxl import load_workbook
from openpyxl.chart import LineChart, Reference
from openpyxl.utils.dataframe import dataframe_to_rows
# 读取Excel文件
filename = 'document.xlsx'
df = pd.read_excel(filename)
# 添加供应商
def add_supplier(supplier_name):
new_row = pd.DataFrame({'序号': [len(df) + 1], '项目名称': [''], '供应商名称': [supplier_name], '需求总条数': [0], 'FC条数': [0], 'NC条数': [0]})
df = df.append(new_row, ignore_index=True)
return df
# 修改供应商的FC条数和NC条数
def update_supplier(supplier_name, fc_num, nc_num):
df.loc[df['供应商名称'] == supplier_name, 'FC条数'] = fc_num
df.loc[df['供应商名称'] == supplier_name, 'NC条数'] = nc_num
return df
# 删除供应商的数据
def delete_supplier(supplier_name):
df = df[df['供应商名称'] != supplier_name].reset_index(drop=True)
return df
# 根据FC条数和NC条数排序
def sort_by_fc():
df = df.sort_values(by=['FC条数'], ascending=False).reset_index(drop=True)
return df
def sort_by_nc():
df = df.sort_values(by=['NC条数'], ascending=False).reset_index(drop=True)
return df
# 查看某个供应商的数据
def view_supplier(supplier_name):
supplier_data = df[df['供应商名称'] == supplier_name]
return supplier_data
# 生成数据透视表和折线图
def generate_pivot_chart():
# 生成数据透视表
pivot_by_fc = pd.pivot_table(df, index=['供应商名称'], values=['FC条数'], aggfunc=sum)
pivot_by_nc = pd.pivot_table(df, index=['供应商名称'], values=['NC条数'], aggfunc=sum)
# 将数据透视表写入新的sheet页
book = load_workbook(filename)
writer = pd.ExcelWriter(filename, engine='openpyxl')
writer.book = book
pivot_by_fc.to_excel(writer, sheet_name='FC透视表')
pivot_by_nc.to_excel(writer, sheet_name='NC透视表')
# 生成折线图
chart_by_fc = LineChart()
chart_by_fc.title = '供应商FC条数'
chart_by_fc.y_axis.title = '条数'
chart_by_fc.x_axis.title = '供应商'
data = Reference(book['FC透视表'], min_col=2, min_row=2, max_col=2, max_row=len(pivot_by_fc) + 1)
categories = Reference(book['FC透视表'], min_col=1, min_row=2, max_row=len(pivot_by_fc) + 1)
chart_by_fc.add_data(data, titles_from_data=True)
chart_by_fc.set_categories(categories)
chart_by_fc.height = 10
chart_by_fc.width = 20
chart_by_fc.legend = None
chart_by_fc.x_axis.tickLblSkip = 0
book.create_sheet('FC折线图')
book['FC折线图'].add_chart(chart_by_fc)
chart_by_nc = LineChart()
chart_by_nc.title = '供应商NC条数'
chart_by_nc.y_axis.title = '条数'
chart_by_nc.x_axis.title = '供应商'
data = Reference(book['NC透视表'], min_col=2, min_row=2, max_col=2, max_row=len(pivot_by_nc) + 1)
categories = Reference(book['NC透视表'], min_col=1, min_row=2, max_row=len(pivot_by_nc) + 1)
chart_by_nc.add_data(data, titles_from_data=True)
chart_by_nc.set_categories(categories)
chart_by_nc.height = 10
chart_by_nc.width = 20
chart_by_nc.legend = None
chart_by_nc.x_axis.tickLblSkip = 0
book.create_sheet('NC折线图')
book['NC折线图'].add_chart(chart_by_nc)
writer.save()
# 输入合法性校验
def check_input(supplier_name, fc_num, nc_num):
if not isinstance(fc_num, int) or not isinstance(nc_num, int) or fc_num < 0 or nc_num < 0:
return False
if supplier_name in df['供应商名称'].unique():
return True
else:
return False
# 测试代码
supplier_name = '供应商A'
fc_num = 10
nc_num = 20
df = add_supplier(supplier_name)
df = update_supplier(supplier_name, fc_num, nc_num)
df = delete_supplier(supplier_name)
df = sort_by_fc()
df = sort_by_nc()
supplier_data = view_supplier(supplier_name)
generate_pivot_chart()
valid_input = check_input(supplier_name, fc_num, nc_num)
```
阅读全文