含main函数的完整python代码实现: 1. 创建一个excel,添加多条需求信息,A列需求序号,B列需求内容,C列需求提出人,D列需求状态(open/close),E列需求提出时间,F列需求关闭时间 2. 后续可以新增需求 3. 可根据C列需求提出人和D列需求状态查询excel中的需求 4. 可以修改指定需求提出人且指定的状态为open的需求的状态,同时修改该需求的需求关闭时间为当前时间 5. 可以删除需求提出人为指定人且需求状态为close的需求 6. 可以统计出当前总的open需求的数量
时间: 2023-10-17 11:31:52 浏览: 121
```python
import openpyxl
from openpyxl import Workbook
from datetime import datetime
class RequirementManagementSystem:
def __init__(self, file_name):
self.file_name = file_name
self.wb = Workbook()
self.ws = self.wb.active
self.ws.title = "Requirements"
self.ws.append(["Req ID", "Req Content", "Req Raised By", "Req Status", "Req Raised Time", "Req Closed Time"])
self.wb.save(self.file_name)
def add_requirement(self, req_id, req_content, req_raised_by):
self.wb = openpyxl.load_workbook(self.file_name)
self.ws = self.wb.active
req_status = "open"
req_raised_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
self.ws.append([req_id, req_content, req_raised_by, req_status, req_raised_time, ""])
self.wb.save(self.file_name)
print("Requirement added successfully!")
def query_requirement(self, req_raised_by=None, req_status=None):
self.wb = openpyxl.load_workbook(self.file_name)
self.ws = self.wb.active
req_list = []
for row in range(2, self.ws.max_row + 1):
if req_raised_by and req_raised_by != self.ws.cell(row=row, column=3).value:
continue
if req_status and req_status != self.ws.cell(row=row, column=4).value:
continue
req_list.append([self.ws.cell(row=row, column=1).value,
self.ws.cell(row=row, column=2).value,
self.ws.cell(row=row, column=3).value,
self.ws.cell(row=row, column=4).value,
self.ws.cell(row=row, column=5).value,
self.ws.cell(row=row, column=6).value])
return req_list
def update_requirement(self, req_raised_by, req_status):
self.wb = openpyxl.load_workbook(self.file_name)
self.ws = self.wb.active
for row in range(2, self.ws.max_row + 1):
if req_raised_by == self.ws.cell(row=row, column=3).value and req_status == self.ws.cell(row=row, column=4).value:
self.ws.cell(row=row, column=4).value = "close"
self.ws.cell(row=row, column=6).value = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
self.wb.save(self.file_name)
print("Requirement updated successfully!")
def delete_requirement(self, req_raised_by):
self.wb = openpyxl.load_workbook(self.file_name)
self.ws = self.wb.active
rows_to_delete = []
for row in range(2, self.ws.max_row + 1):
if req_raised_by == self.ws.cell(row=row, column=3).value and "close" == self.ws.cell(row=row, column=4).value:
rows_to_delete.append(row)
rows_to_delete.sort(reverse=True)
for row in rows_to_delete:
self.ws.delete_rows(row)
self.wb.save(self.file_name)
print("Requirement deleted successfully!")
def count_open_requirements(self):
self.wb = openpyxl.load_workbook(self.file_name)
self.ws = self.wb.active
count = 0
for row in range(2, self.ws.max_row + 1):
if self.ws.cell(row=row, column=4).value == "open":
count += 1
return count
# 测试代码
rms = RequirementManagementSystem("requirements.xlsx")
rms.add_requirement("R001", "Requirement 1", "Alice")
rms.add_requirement("R002", "Requirement 2", "Bob")
rms.add_requirement("R003", "Requirement 3", "Charlie")
rms.add_requirement("R004", "Requirement 4", "Alice")
rms.add_requirement("R005", "Requirement 5", "David")
rms.add_requirement("R006", "Requirement 6", "Bob")
rms.add_requirement("R007", "Requirement 7", "Eva")
print("Query by raised by Alice:")
print(rms.query_requirement(req_raised_by="Alice"))
print("Query by status open:")
print(rms.query_requirement(req_status="open"))
rms.update_requirement("Alice", "open")
print("Query by status close after updating Alice's requirements:")
print(rms.query_requirement(req_status="close"))
rms.delete_requirement("Bob")
print("Query by status close after deleting Bob's requirements:")
print(rms.query_requirement(req_status="close"))
print("Total open requirements:", rms.count_open_requirements())
```
阅读全文