如下需求管理系统的完整python代码实现: 1. 创建一个excel,可添加需求信息,A列需求序号,B列需求内容,C列需求提出人,D列需求状态(open/close) 2. 可根据B列关键字查询excel中的需求 3. 可以根据D列需求状态查询open的需求 4. 可以修改指定需求的状态
时间: 2024-03-24 10:38:58 浏览: 60
以下是完整的 Python 代码实现需求管理系统的功能,使用了 openpyxl 库进行 Excel 文件的读写:
```python
import openpyxl
# 创建 Excel 文件和工作表
workbook = openpyxl.Workbook()
sheet = workbook.active
sheet.title = "需求列表"
sheet["A1"] = "序号"
sheet["B1"] = "需求内容"
sheet["C1"] = "提出人"
sheet["D1"] = "状态"
# 添加需求信息
def add_requirement(requirement_num, requirement_content, requirement_proposer, requirement_status):
row_num = sheet.max_row + 1
sheet.cell(row=row_num, column=1).value = requirement_num
sheet.cell(row=row_num, column=2).value = requirement_content
sheet.cell(row=row_num, column=3).value = requirement_proposer
sheet.cell(row=row_num, column=4).value = requirement_status
# 根据需求内容关键字查询需求
def search_requirement_by_content(keyword):
result = []
for row in sheet.iter_rows(min_row=2, values_only=True):
if keyword in row[1]:
result.append(row)
return result
# 根据需求状态查询需求
def search_requirement_by_status(status):
result = []
for row in sheet.iter_rows(min_row=2, values_only=True):
if row[3] == status:
result.append(row)
return result
# 修改指定需求的状态
def update_requirement_status(requirement_num, status):
for row in sheet.iter_rows(min_row=2):
if row[0].value == requirement_num:
row[3].value = status
# 测试代码
add_requirement(1, "需求1", "提出人1", "open")
add_requirement(2, "需求2", "提出人2", "close")
add_requirement(3, "需求3", "提出人3", "open")
result = search_requirement_by_content("需求")
print("根据关键字查询结果:")
for row in result:
print(row)
result = search_requirement_by_status("open")
print("根据状态查询结果:")
for row in result:
print(row)
update_requirement_status(1, "close")
print("修改状态后的需求列表:")
for row in sheet.iter_rows(min_row=1, values_only=True):
print(row)
# 保存 Excel 文件
workbook.save("需求列表.xlsx")
```
使用示例:
1. 添加需求信息:
```python
add_requirement(1, "需求1", "提出人1", "open")
```
2. 根据关键字查询需求:
```python
result = search_requirement_by_content("需求")
for row in result:
print(row)
```
3. 根据状态查询需求:
```python
result = search_requirement_by_status("open")
for row in result:
print(row)
```
4. 修改指定需求的状态:
```python
update_requirement_status(1, "close")
```
5. 保存 Excel 文件:
```python
workbook.save("需求列表.xlsx")
```
阅读全文