alarm_list = [] alarm_dict = { "disk": "Disk", "cpu": "CPU", "memory": "Mem", "proc": "proc", "activemq": "active", "disk_io": "disk_io", } for file in dir_lists: if file.find(alarm_dict[alarm]) != -1: alarm_list.append(file) return alarm_list
对disk项进行分类并按照时间排序
在word文档中找出对应文字的位置,并将前一行的行号加入数组,用于插入图片
1 2 3 4 5 6
graph_line_numbers = [] for i, paragraph in enumerate(document.paragraphs): line = str(i) + ':' + paragraph.text if paragraph.text.find(目录使用情况) != -1: add_graph_line_numbers = int(line.split(":")[0]) - 1 graph_line_numbers.append(add_graph_line_numbers)
将相应的图片插入文档中,并设置合适的宽度,这里为6英寸
1 2 3 4 5 6
#get_info_sort_time方法为上文分类创建的函数 infos = get_info_sort_time(disk) for number, info in zip(graph_line_numbers, infos): para = document.paragraphs[int(number)] run = para.add_run() run.add_picture(info, width=Inches(6))
可以修改文档中的时间(文档中的时间为01.17)
1 2 3 4 5
document_time = str(datetime.datetime.now().strftime('%m.%d')) for paragraph in document.paragraphs: for run in paragraph.runs: if"01.17"in run.text: run.text = run.text.replace('01.17', document_time)
修改word图表中的时间
1 2 3 4 5 6 7
for table in document.tables: for row in table.rows: # 读表的每行 for cell in row.cells: # 读一行中的所有单元格 for paragraph in cell.paragraphs: # 读单元格的所有段落 if paragraph.text.split(): if paragraph.text.find('2021/01/17') != -1: paragraph.text = paragraph.text.replace('2021/01/17', str(datetime.datetime.now().strftime( '%Y/%m/%d' )))