含main函数的python代码实现: 1. 创建一个excel,可添加多条需求信息,A列需求序号,B列需求内容,C列需求提出人,D列需求状态(open/close),E列需求提出时间,F列需求关闭时间 2. 可根据C列需求提出人查询excel中的需求 3. 可以根据D列需求状态查询open的需求 4. 可以根据E列需求提出时间查询符合条件的需求 5. 可以修改需求提出人为马超且指定的状态为open的需求的状态,从open改为close,同时修改该需求的需求关闭时间为当前时间 6. 可以删除需求提出人为指定人且需求状态为close的需求 7. 可以统计出当前总的open需求的数量 8. 可以统计出当前每个需求提出人名下open的需求数量和close的需求数量
时间: 2023-10-17 10:31:52 浏览: 78
python实现多进程按序号批量修改文件名的方法示例
很抱歉,Python并没有原生支持Excel操作,因此需要安装第三方库openpyxl来实现该需求。以下是实现该需求的Python代码:
```python
import openpyxl
from openpyxl.utils import get_column_letter
from datetime import datetime
def create_excel(filename):
# 创建工作簿
wb = openpyxl.Workbook()
# 选择默认的工作表
ws = wb.active
# 创建表头
ws['A1'] = '需求序号'
ws['B1'] = '需求内容'
ws['C1'] = '需求提出人'
ws['D1'] = '需求状态'
ws['E1'] = '需求提出时间'
ws['F1'] = '需求关闭时间'
# 设置列宽
for col in ws.columns:
column = col[0].column_letter
ws.column_dimensions[column].width = 20
# 保存Excel文件
wb.save(filename)
def add_demand(filename, demand):
# 打开工作簿
wb = openpyxl.load_workbook(filename)
# 选择默认的工作表
ws = wb.active
# 获取当前最大行号
max_row = ws.max_row
# 在最大行号的下一行添加需求信息
ws.cell(row=max_row+1, column=1, value=demand['序号'])
ws.cell(row=max_row+1, column=2, value=demand['内容'])
ws.cell(row=max_row+1, column=3, value=demand['提出人'])
ws.cell(row=max_row+1, column=4, value=demand['状态'])
ws.cell(row=max_row+1, column=5, value=demand['提出时间'].strftime('%Y-%m-%d %H:%M:%S'))
ws.cell(row=max_row+1, column=6, value=demand['关闭时间'].strftime('%Y-%m-%d %H:%M:%S') if demand['关闭时间'] else '')
# 保存Excel文件
wb.save(filename)
def search_by_producer(filename, producer):
# 打开工作簿
wb = openpyxl.load_workbook(filename)
# 选择默认的工作表
ws = wb.active
# 遍历C列,查找符合条件的需求
demands = []
for cell in ws['C']:
if cell.value == producer:
demand = {}
row = cell.row
demand['序号'] = ws.cell(row=row, column=1).value
demand['内容'] = ws.cell(row=row, column=2).value
demand['提出人'] = ws.cell(row=row, column=3).value
demand['状态'] = ws.cell(row=row, column=4).value
demand['提出时间'] = datetime.strptime(ws.cell(row=row, column=5).value, '%Y-%m-%d %H:%M:%S')
demand['关闭时间'] = datetime.strptime(ws.cell(row=row, column=6).value, '%Y-%m-%d %H:%M:%S') if ws.cell(row=row, column=6).value else None
demands.append(demand)
return demands
def search_by_status(filename, status):
# 打开工作簿
wb = openpyxl.load_workbook(filename)
# 选择默认的工作表
ws = wb.active
# 遍历D列,查找符合条件的需求
demands = []
for cell in ws['D']:
if cell.value == status:
demand = {}
row = cell.row
demand['序号'] = ws.cell(row=row, column=1).value
demand['内容'] = ws.cell(row=row, column=2).value
demand['提出人'] = ws.cell(row=row, column=3).value
demand['状态'] = ws.cell(row=row, column=4).value
demand['提出时间'] = datetime.strptime(ws.cell(row=row, column=5).value, '%Y-%m-%d %H:%M:%S')
demand['关闭时间'] = datetime.strptime(ws.cell(row=row, column=6).value, '%Y-%m-%d %H:%M:%S') if ws.cell(row=row, column=6).value else None
demands.append(demand)
return demands
def search_by_time(filename, start_time, end_time):
# 打开工作簿
wb = openpyxl.load_workbook(filename)
# 选择默认的工作表
ws = wb.active
# 遍历E列,查找符合条件的需求
demands = []
for cell in ws['E']:
demand_time = datetime.strptime(cell.value, '%Y-%m-%d %H:%M:%S')
if demand_time >= start_time and demand_time <= end_time:
demand = {}
row = cell.row
demand['序号'] = ws.cell(row=row, column=1).value
demand['内容'] = ws.cell(row=row, column=2).value
demand['提出人'] = ws.cell(row=row, column=3).value
demand['状态'] = ws.cell(row=row, column=4).value
demand['提出时间'] = demand_time
demand['关闭时间'] = datetime.strptime(ws.cell(row=row, column=6).value, '%Y-%m-%d %H:%M:%S') if ws.cell(row=row, column=6).value else None
demands.append(demand)
return demands
def modify_demand(filename, producer, status):
# 打开工作簿
wb = openpyxl.load_workbook(filename)
# 选择默认的工作表
ws = wb.active
# 遍历C列和D列,查找符合条件的需求
for cell in ws['C']:
if cell.value == producer:
row = cell.row
if ws.cell(row=row, column=4).value == 'open':
ws.cell(row=row, column=4, value='close')
ws.cell(row=row, column=6, value=datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
# 保存Excel文件
wb.save(filename)
def delete_demand(filename, producer):
# 打开工作簿
wb = openpyxl.load_workbook(filename)
# 选择默认的工作表
ws = wb.active
# 遍历C列和D列,查找符合条件的需求
for cell in ws['C']:
if cell.value == producer:
row = cell.row
if ws.cell(row=row, column=4).value == 'close':
ws.delete_rows(row)
# 保存Excel文件
wb.save(filename)
def count_open_demands(filename):
# 打开工作簿
wb = openpyxl.load_workbook(filename)
# 选择默认的工作表
ws = wb.active
# 统计open状态的需求数量
count = 0
for cell in ws['D']:
if cell.value == 'open':
count += 1
return count
def count_demands_by_producer(filename):
# 打开工作簿
wb = openpyxl.load_workbook(filename)
# 选择默认的工作表
ws = wb.active
# 统计每个需求提出人名下open和close状态的需求数量
demands = {}
for cell in ws['C']:
producer = cell.value
if producer not in demands:
demands[producer] = {'open': 0, 'close': 0}
row = cell.row
if ws.cell(row=row, column=4).value == 'open':
demands[producer]['open'] += 1
else:
demands[producer]['close'] += 1
return demands
```
该代码实现了所需求的所有功能,以下是使用该代码的示例:
```python
# 创建Excel文件
create_excel('demands.xlsx')
# 添加需求
demand1 = {'序号': 1, '内容': '需求1', '提出人': '张三', '状态': 'open', '提出时间': datetime.now(), '关闭时间': None}
add_demand('demands.xlsx', demand1)
demand2 = {'序号': 2, '内容': '需求2', '提出人': '李四', '状态': 'open', '提出时间': datetime.now(), '关闭时间': None}
add_demand('demands.xlsx', demand2)
demand3 = {'序号': 3, '内容': '需求3', '提出人': '张三', '状态': 'close', '提出时间': datetime.now(), '关闭时间': datetime.now()}
add_demand('demands.xlsx', demand3)
# 根据需求提出人查询需求
demands = search_by_producer('demands.xlsx', '张三')
print(demands)
# 根据需求状态查询open的需求
demands = search_by_status('demands.xlsx', 'open')
print(demands)
# 根据需求提出时间查询符合条件的需求
start_time = datetime(year=2021, month=1, day=1)
end_time = datetime.now()
demands = search_by_time('demands.xlsx', start_time, end_time)
print(demands)
# 修改需求状态为close并更新需求关闭时间
modify_demand('demands.xlsx', '张三', 'open')
# 删除关闭的需求
delete_demand('demands.xlsx', '张三')
# 统计open状态的需求数量
count = count_open_demands('demands.xlsx')
print(count)
# 统计每个需求提出人名下open和close状态的需求数量
demands = count_demands_by_producer('demands.xlsx')
print(demands)
```
阅读全文