python代码: 1. 创建一个需求管理excel,如果已经存在则直接打开,可添加50条需求,A列需求序号,B列需求内容,C列需求提出人,D列需求状态(open/close),E列需求提出时间,F列需求关闭时间 2. 可根据C列需求提
时间: 2023-09-15 12:19:43 浏览: 65
出人进行筛选和排序,也可以根据D列需求状态进行筛选和排序。
下面是Python代码实现:
```python
import openpyxl
from openpyxl import Workbook
from openpyxl.utils import get_column_letter
from openpyxl.styles import Font
from datetime import datetime
class DemandManagement:
def __init__(self, filename):
try:
self.workbook = openpyxl.load_workbook(filename)
except FileNotFoundError:
self.workbook = Workbook()
self.workbook.create_sheet(title="Demands", index=0)
self.worksheet = self.workbook.active
self.worksheet.title = "Demands"
self.columns = ["A", "B", "C", "D", "E", "F"]
self.current_row = 1
self.max_rows = 50
self.header_font = Font(bold=True)
self.create_header()
def create_header(self):
headers = ["需求序号", "需求内容", "需求提出人", "需求状态", "需求提出时间", "需求关闭时间"]
for col_num, header in enumerate(headers, 1):
column_letter = get_column_letter(col_num)
cell = self.worksheet["{}1".format(column_letter)]
cell.value = header
cell.font = self.header_font
def add_demand(self, demand_content, demand_raiser):
if self.current_row > self.max_rows:
return "Max rows reached."
self.current_row += 1
demand_number = self.current_row - 1
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
self.worksheet["A{}".format(self.current_row)] = demand_number
self.worksheet["B{}".format(self.current_row)] = demand_content
self.worksheet["C{}".format(self.current_row)] = demand_raiser
self.worksheet["D{}".format(self.current_row)] = "open"
self.worksheet["E{}".format(self.current_row)] = timestamp
self.workbook.save("demand_management.xlsx")
return "Demand added successfully."
def filter_by_raiser(self, demand_raiser):
demands = []
for row in self.worksheet.iter_rows(min_row=2, values_only=True):
if row[2] == demand_raiser:
demands.append(row)
return demands
def filter_by_status(self, demand_status):
demands = []
for row in self.worksheet.iter_rows(min_row=2, values_only=True):
if row[3] == demand_status:
demands.append(row)
return demands
def sort_by_raiser(self):
sorted_rows = sorted(self.worksheet.iter_rows(min_row=2, values_only=True), key=lambda x: x[2])
return sorted_rows
def sort_by_status(self):
sorted_rows = sorted(self.worksheet.iter_rows(min_row=2, values_only=True), key=lambda x: x[3])
return sorted_rows
```
使用方法:
```python
dm = DemandManagement("demand_management.xlsx")
dm.add_demand("需求内容1", "提出人1")
dm.add_demand("需求内容2", "提出人2")
dm.add_demand("需求内容3", "提出人3")
# 根据提出人筛选需求
demands_by_raiser = dm.filter_by_raiser("提出人1")
print(demands_by_raiser)
# 根据状态筛选需求
demands_by_status = dm.filter_by_status("open")
print(demands_by_status)
# 根据提出人排序需求
sorted_demands_by_raiser = dm.sort_by_raiser()
print(sorted_demands_by_raiser)
# 根据状态排序需求
sorted_demands_by_status = dm.sort_by_status()
print(sorted_demands_by_status)
```
阅读全文