python完整代码 需求管理系统: 1. 创建一个excel,可添加需求信息,A列需求序号,B列需求内容,C列需求提出人,D列需求状态(open/close) 2. 可根据B列关键字查询excel中的需求 3. 可以根据D列需求状态查询open的需求 4. 可以修改指定需求的状态
时间: 2024-03-24 19:38:57 浏览: 126
以下是一个简单的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")
```
阅读全文