python代码: 1. 使用excel进行文档管理,A列序号,B列项目名称,C列文档名称,D列供应商名称,E列需求总条数,F列FC条数 2. 可以添加文档,修改某个供应商某个文档的FC条数 3. 可以对同一个文档根据FC条数进行排序 4. 可以查看某个供应商的数据 5. 需要有输入合法性校验 6. 需要根据C, D, F列生成数据透视图到新的sheet页
时间: 2024-03-25 14:38:43 浏览: 107
以下是一个基本的实现,需要安装 openpyxl 库。
```python
from openpyxl import Workbook, load_workbook
from openpyxl.utils import column_index_from_string
from openpyxl.styles import Font, Alignment
# 设置文档路径
file_path = "example.xlsx"
# 检查文档是否存在,不存在则新建
try:
wb = load_workbook(filename=file_path)
except:
wb = Workbook()
ws = wb.active
ws.title = "Sheet1"
ws.append(["序号", "项目名称", "文档名称", "供应商名称", "需求总条数", "FC条数"])
ws.column_dimensions["A"].width = 8
ws.column_dimensions["B"].width = 30
ws.column_dimensions["C"].width = 30
ws.column_dimensions["D"].width = 20
ws.column_dimensions["E"].width = 12
ws.column_dimensions["F"].width = 12
title_font = Font(name="Calibri", size=12, bold=True)
title_alignment = Alignment(horizontal="center", vertical="center")
for col in ws.columns:
col[0].font = title_font
col[0].alignment = title_alignment
wb.save(file_path)
# 读取数据
ws = wb.active
def get_data():
data = []
for row in ws.iter_rows(min_row=2):
item = {}
for i, cell in enumerate(row):
item[ws.cell(row=1, column=cell.column).value] = cell.value
data.append(item)
return data
# 添加文档
def add_data(data):
# 检查序号是否已存在
for row in ws.iter_rows(min_row=2):
if row[0].value == data["序号"]:
raise ValueError("序号已存在!")
# 添加数据
row = [data.get("序号"), data.get("项目名称"), data.get("文档名称"), data.get("供应商名称"), data.get("需求总条数"), data.get("FC条数")]
ws.append(row)
wb.save(file_path)
# 修改FC条数
def update_fc(supplier_name, document_name, fc):
# 查找数据
for row in ws.iter_rows(min_row=2):
if row[3].value == supplier_name and row[2].value == document_name:
row[5].value = fc
wb.save(file_path)
return
raise ValueError("找不到指定数据!")
# 排序
def sort_by_fc():
ws.sort_range(min_row=2, max_row=ws.max_row, min_col=column_index_from_string("F"), max_col=column_index_from_string("F"), descending=True)
wb.save(file_path)
# 查看供应商数据
def view_supplier_data(supplier_name):
data = []
for row in ws.iter_rows(min_row=2):
if row[3].value == supplier_name:
item = {}
for i, cell in enumerate(row):
item[ws.cell(row=1, column=cell.column).value] = cell.value
data.append(item)
return data
# 生成数据透视图
def generate_pivot_table():
from openpyxl.utils import get_column_letter
from openpyxl.worksheet.pivot_table import PivotTable, PivotTableStyleInfo
from openpyxl.utils.dataframe import dataframe_to_rows
import pandas as pd
data = get_data()
df = pd.DataFrame(data)
pt = PivotTable(df, rows=["文档名称"], cols=["供应商名称"], values=["FC条数"], aggfunc="sum")
# 清空原有数据透视表
while "PivotTable" in wb.sheetnames:
wb.remove(wb["PivotTable"])
# 新建数据透视表
ws = wb.create_sheet("PivotTable")
for r in dataframe_to_rows(pt):
ws.append(r)
# 设置样式
style = PivotTableStyleInfo(name="TableStyleLight9", showRowStripes=True, showColumnStripes=True)
pt = ws._pivots[-1]
pt.cacheDefinition = None
pt.pivotTableStyleInfo = style
wb.save(file_path)
```
例子中的操作都是通过调用函数实现的,具体使用方式可以参考以下代码:
```python
# 添加文档
add_data({
"序号": 1,
"项目名称": "项目1",
"文档名称": "文档1",
"供应商名称": "供应商1",
"需求总条数": 100,
"FC条数": 10
})
# 修改FC条数
update_fc("供应商1", "文档1", 20)
# 排序
sort_by_fc()
# 查看供应商数据
data = view_supplier_data("供应商1")
print(data)
# 生成数据透视表
generate_pivot_table()
```
阅读全文