python代码: 1. 创建一个供应商答标管理excel,如果已经存在则直接打开,可添加多个供应商数据,A列序号,B列项目名称,C列供应商名称,D列需求总条数,E列FC条数,F列NC条数 2. 可以修改某个供应商的FC条数,NC条数 3. 可以新增一个供应商的数据 4. 可以删除某个供应商的数据 5. 可以对同一个文档根据FC条数进行排序 6. 可以对同一个文档根据NC条数进行排序 7. 可以查看某个供应商的数据 8. 有输入合法性校验 9. 根据C, E列生成数据透视图到新的sheet页 10. 根据C, F列生成数据透视图到新的sheet页 11. 根据C, E列生成折线图到新的sheet页 12. 根据C, F列生成折线图到新的sheet页 13. 根据C,E,F列生成柱状图到新的sheet页
时间: 2024-03-25 12:41:09 浏览: 143
以下是实现上述功能的Python代码,需要安装openpyxl和matplotlib库:
```python
import openpyxl
from openpyxl.chart import (
LineChart,
Reference,
BarChart
)
from openpyxl.chart.axis import DateAxis
from openpyxl.utils import (
column_index_from_string,
get_column_letter
)
import matplotlib.pyplot as plt
def create_excel(filename):
try:
wb = openpyxl.load_workbook(filename)
except:
wb = openpyxl.Workbook()
wb.save(filename)
ws = wb.active
ws.title = "供应商答标管理"
ws.append(["序号", "项目名称", "供应商名称", "需求总条数", "FC条数", "NC条数"])
wb.save(filename)
return wb
def add_supplier(filename, data):
wb = openpyxl.load_workbook(filename)
ws = wb["供应商答标管理"]
max_row = ws.max_row
ws.append([max_row, data[0], data[1], data[2], data[3], data[4]])
wb.save(filename)
def delete_supplier(filename, supplier_name):
wb = openpyxl.load_workbook(filename)
ws = wb["供应商答标管理"]
for row in ws.iter_rows(min_row=2, values_only=True):
if row[2] == supplier_name:
ws.delete_rows(row[0]+1)
break
wb.save(filename)
def modify_fc_nc(filename, supplier_name, fc_count, nc_count):
wb = openpyxl.load_workbook(filename)
ws = wb["供应商答标管理"]
for row in ws.iter_rows(min_row=2, values_only=True):
if row[2] == supplier_name:
ws.cell(row[0]+1, 5).value = fc_count
ws.cell(row[0]+1, 6).value = nc_count
break
wb.save(filename)
def sort_by_fc(filename):
wb = openpyxl.load_workbook(filename)
ws = wb["供应商答标管理"]
data = []
for row in ws.iter_rows(min_row=2, values_only=True):
data.append(row)
data.sort(key=lambda x: x[4])
ws.delete_rows(2, ws.max_row)
for row in data:
ws.append(row)
wb.save(filename)
def sort_by_nc(filename):
wb = openpyxl.load_workbook(filename)
ws = wb["供应商答标管理"]
data = []
for row in ws.iter_rows(min_row=2, values_only=True):
data.append(row)
data.sort(key=lambda x: x[5])
ws.delete_rows(2, ws.max_row)
for row in data:
ws.append(row)
wb.save(filename)
def view_supplier_data(filename, supplier_name):
wb = openpyxl.load_workbook(filename)
ws = wb["供应商答标管理"]
for row in ws.iter_rows(min_row=2, values_only=True):
if row[2] == supplier_name:
print("序号:", row[0])
print("项目名称:", row[1])
print("供应商名称:", row[2])
print("需求总条数:", row[3])
print("FC条数:", row[4])
print("NC条数:", row[5])
break
def generate_pivot_table(filename):
wb = openpyxl.load_workbook(filename)
ws = wb["供应商答标管理"]
pivot_sheet_e = wb.create_sheet("数据透视表_E")
pivot_sheet_f = wb.create_sheet("数据透视表_F")
data = Reference(ws, min_col=1, min_row=1, max_col=6, max_row=ws.max_row)
pivot_table_e = pivot_sheet_e.pivot_table(data, rows="供应商名称", values="FC条数", aggfunc="sum")
pivot_table_f = pivot_sheet_f.pivot_table(data, rows="供应商名称", values="NC条数", aggfunc="sum")
for i in range(len(pivot_table_e)):
pivot_sheet_e.cell(row=i+2, column=1, value=pivot_table_e.index[i])
pivot_sheet_e.cell(row=i+2, column=2, value=pivot_table_e.values[i][0])
for i in range(len(pivot_table_f)):
pivot_sheet_f.cell(row=i+2, column=1, value=pivot_table_f.index[i])
pivot_sheet_f.cell(row=i+2, column=2, value=pivot_table_f.values[i][0])
wb.save(filename)
def generate_line_chart(filename):
wb = openpyxl.load_workbook(filename)
ws = wb["供应商答标管理"]
line_chart_e = LineChart()
line_chart_e.title = "FC条数折线图"
line_chart_e.y_axis.title = "FC条数"
line_chart_e.x_axis.title = "日期"
line_chart_e.style = 13
line_chart_f = LineChart()
line_chart_f.title = "NC条数折线图"
line_chart_f.y_axis.title = "NC条数"
line_chart_f.x_axis.title = "日期"
line_chart_f.style = 13
data = Reference(ws, min_col=1, min_row=2, max_col=6, max_row=ws.max_row)
dates = Reference(ws, min_col=4, min_row=2, max_row=ws.max_row)
for i in range(5, 7):
values = Reference(ws, min_col=i, min_row=2, max_row=ws.max_row)
series = line_chart_e.series[0] if i == 5 else line_chart_f.series[0]
series.title = ws.cell(row=1, column=i).value
series.marker.symbol = "circle"
series.marker.size = 5
series.marker.graphicalProperties.solidFill = "FFFFFF"
series.marker.graphicalProperties.line.solidFill = ws.cell(row=1, column=i).fill.start_color.index
series.graphicalProperties.line.noFill = True
series.graphicalProperties.line.dashStyle = "sysDot"
series.graphicalProperties.line.width = 2.25
series.graphicalProperties.line.solidFill = ws.cell(row=1, column=i).fill.start_color.index
line_chart_e.y_axis.crosses = "min"
line_chart_e.x_axis = DateAxis(crosses="max")
line_chart_e.x_axis.number_format = 'd-mmm'
line_chart_f.y_axis.crosses = "min"
line_chart_f.x_axis = DateAxis(crosses="max")
line_chart_f.x_axis.number_format = 'd-mmm'
series.values = values
series.categories = dates
chart_sheet_e = wb.create_sheet("FC条数折线图")
chart_sheet_f = wb.create_sheet("NC条数折线图")
chart_sheet_e.add_chart(line_chart_e, "A1")
chart_sheet_f.add_chart(line_chart_f, "A1")
wb.save(filename)
def generate_bar_chart(filename):
wb = openpyxl.load_workbook(filename)
ws = wb["供应商答标管理"]
bar_chart = BarChart()
bar_chart.title = "FC/NC条数柱状图"
bar_chart.y_axis.title = "条数"
bar_chart.x_axis.title = "供应商名称"
bar_chart.style = 13
data = Reference(ws, min_col=3, min_row=2, max_col=6, max_row=ws.max_row)
bar_chart.categories = Reference(ws, min_col=3, min_row=2, max_row=ws.max_row)
for i in range(5, 7):
values = Reference(ws, min_col=i, min_row=2, max_row=ws.max_row)
series = bar_chart.series[0] if i == 5 else bar_chart.series[1]
series.title = ws.cell(row=1, column=i).value
series.graphicalProperties.solidFill = ws.cell(row=1, column=i).fill.start_color.index
series.graphicalProperties.line.noFill = True
series.graphicalProperties.line.dashStyle = "sysDot"
series.graphicalProperties.line.width = 2.25
series.values = values
chart_sheet = wb.create_sheet("FC/NC条数柱状图")
chart_sheet.add_chart(bar_chart, "A1")
wb.save(filename)
if __name__ == "__main__":
filename = "供应商答标管理.xlsx"
wb = create_excel(filename)
add_supplier(filename, ["项目1", "供应商A", 100, 50, 50])
add_supplier(filename, ["项目1", "供应商B", 100, 30, 70])
add_supplier(filename, ["项目1", "供应商C", 100, 40, 60])
modify_fc_nc(filename, "供应商A", 60, 40)
delete_supplier(filename, "供应商B")
sort_by_fc(filename)
sort_by_nc(filename)
view_supplier_data(filename, "供应商A")
generate_pivot_table(filename)
generate_line_chart(filename)
generate_bar_chart(filename)
```
阅读全文