python代码: 1. 创建一个需求管理excel,如果已经存在则直接打开,可添加至少10条需求,A列需求序号,B列需求内容,C列需求提出人,D列需求状态(open/close),E列需求提出时间,F列需求关闭时间 2. 可根据C列需求提出人和D列需求状态查询excel中的需求 3. 可以修改指定需求提出人且指定的状态为open的需求的状态,同时修改该需求的需求关闭时间为当前时间 4. 可以删除需求提出人为指定人且需求状态为close的需求 5. 可以统计出open或者close需求的数量
时间: 2023-10-17 18:31:59 浏览: 117
好的,我已经记下您的问题了。以下是对您问题的回答:
1. 代码如下:
```python
import openpyxl
from openpyxl import Workbook
from openpyxl.utils import get_column_letter
from openpyxl.styles import Font, Alignment
from datetime import datetime
# 打开或创建需求管理excel
try:
wb = openpyxl.load_workbook('需求管理.xlsx')
sheet = wb.active
except FileNotFoundError:
wb = Workbook()
sheet = wb.active
sheet.title = '需求管理'
sheet['A1'] = '需求序号'
sheet['B1'] = '需求内容'
sheet['C1'] = '需求提出人'
sheet['D1'] = '需求状态'
sheet['E1'] = '需求提出时间'
sheet['F1'] = '需求关闭时间'
for col in range(1, 7):
col_letter = get_column_letter(col)
sheet.column_dimensions[col_letter].width = 20
sheet.cell(row=1, column=col).font = Font(bold=True)
sheet.cell(row=1, column=col).alignment = Alignment(horizontal='center')
wb.save('需求管理.xlsx')
# 添加需求
def add_requirement(content, proposer):
row = sheet.max_row + 1
now = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
sheet.cell(row=row, column=1).value = row - 1
sheet.cell(row=row, column=2).value = content
sheet.cell(row=row, column=3).value = proposer
sheet.cell(row=row, column=4).value = 'open'
sheet.cell(row=row, column=5).value = now
wb.save('需求管理.xlsx')
# 查询需求
def search_requirement(proposer=None, status=None):
requirements = []
for row in range(2, sheet.max_row + 1):
if (proposer is None or sheet.cell(row=row, column=3).value == proposer) and \
(status is None or sheet.cell(row=row, column=4).value == status):
requirement = {
'序号': sheet.cell(row=row, column=1).value,
'内容': sheet.cell(row=row, column=2).value,
'提出人': sheet.cell(row=row, column=3).value,
'状态': sheet.cell(row=row, column=4).value,
'提出时间': sheet.cell(row=row, column=5).value,
'关闭时间': sheet.cell(row=row, column=6).value
}
requirements.append(requirement)
return requirements
# 修改需求状态和关闭时间
def modify_requirement(proposer, status='open'):
modified = False
for row in range(2, sheet.max_row + 1):
if sheet.cell(row=row, column=3).value == proposer and \
sheet.cell(row=row, column=4).value == 'open':
now = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
sheet.cell(row=row, column=4).value = status
sheet.cell(row=row, column=6).value = now
modified = True
if modified:
wb.save('需求管理.xlsx')
return modified
# 删除需求
def delete_requirement(proposer):
deleted = False
rows_to_delete = []
for row in range(2, sheet.max_row + 1):
if sheet.cell(row=row, column=3).value == proposer and \
sheet.cell(row=row, column=4).value == 'close':
rows_to_delete.append(row)
deleted = True
for row in reversed(rows_to_delete):
sheet.delete_rows(row)
if deleted:
wb.save('需求管理.xlsx')
return deleted
# 统计需求数
def count_requirement(status):
count = 0
for row in range(2, sheet.max_row + 1):
if sheet.cell(row=row, column=4).value == status:
count += 1
return count
```
2. 查询需求的代码如下:
```python
requirements = search_requirement(proposer='张三', status='open')
for requirement in requirements:
print(requirement)
```
3. 修改需求状态和关闭时间的代码如下:
```python
modified = modify_requirement(proposer='张三', status='close')
if modified:
print('修改成功')
else:
print('修改失败')
```
4. 删除需求的代码如下:
```python
deleted = delete_requirement(proposer='张三')
if deleted:
print('删除成功')
else:
print('删除失败')
```
5. 统计需求数的代码如下:
```python
count = count_requirement(status='open')
print('open需求数:', count)
count = count_requirement(status='close')
print('close需求数:', count)
```
阅读全文