Word批量化生成

本文最后更新于:1 分钟前

初测:

批量截图名称保存为:192.168.1.1.png
将其筛选提取出存在漏洞的图片
再提取图片名ip

1
2
3
4
5
6
7
8
9
10
11
12
import os

picturePath = '/Users/aqi/Desktop/python/word批量化生成/pic/' # 图片文件的路径
savePath = "/Users/aqi/Desktop/python/word批量化生成/存在漏洞.txt" # 图片名提取出来后存放的路径 读取图片的名字,保存到txt文件
file_txt = open(savePath,'w') #打开文件,开始文件不存在,当运行这条语句后,会在文件夹里生成txt文件

fileName = os.listdir(picturePath) # 遍历文件夹中的图片名

for i in fileName: # 用for循环遍历文件名
if i.endswith(".png"): # 如果文件是以png结尾的
png_name = i.replace('.png', '') #替换.png为空
file_txt.write(png_name +'\n') # 加入换行符写入txt

运行结果:
img

然后就可以批量word生成了
先准备一个xlsx文件和一个word模板

ip.xlsx:
img

模板.docx:
img

上代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
from docxtpl import InlineImage
from docx.shared import Mm
from docxtpl import DocxTemplate
import pandas as pd

data = pd.read_excel('ip.xlsx') # 读取ip.xlsx
url = data["IP列"]
f = data["序号"]
num = data.shape[0]
for i in range(num) :
tpl = DocxTemplate('模板.docx')

context = {
"f" : f[i],
"url" : url[i],
"pic" : InlineImage(tpl,'/Users/aqi/Desktop/python/word批量化生成/pic/' + url[i] +'.png',width=Mm(150),height=Mm(100))
}
tpl.render(context)
tpl.save("高危-某某系统{}渗透测试报告.docx".format(f[i]))
print("第{}份报告已生成".format(f[i]))

print("已全部结束")

img

复测:

先读取图片,搜索对应ip.xlsx的IP列进行生成报告

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import re
from docxtpl import InlineImage
from docx.shared import Mm
from docxtpl import DocxTemplate
import pandas as pd
import os

data = pd.read_excel('ip.xlsx') # 读取ip.xlsx

pic_folder = '/Users/aqi/Desktop/mysql/root root/pic/' # 图片文件夹路径

for filename in os.listdir(pic_folder):
if filename.endswith('.png'):
ip_match = re.search(r'(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})', filename) # 使用正则表达式提取 IP 地址
if ip_match:
ip = ip_match.group(0)
row = data[data['IP列'] == ip]

if not row.empty:
f = row['序号'].values[0]
tpl = DocxTemplate('未修复-3306模板.docx')
#tpl = DocxTemplate('已修复-3306模板.docx')

context = {
"f": f,
"url": ip,
"pic": InlineImage(tpl, os.path.join(pic_folder, filename), width=Mm(147), height=Mm(75))
}
tpl.render(context)
tpl.save("未修复-MySQL系统{}渗透测试复测报告.docx".format(f))
#tpl.save("已修复-MySQL系统{}渗透测试复测报告.docx".format(f))
print("IP地址为 {} 的报告已生成".format(ip))
else:
print("未找到匹配 IP 地址 {} 的数据".format(ip))
else:
print("无法从文件名中提取 IP 地址:{}".format(filename))

print("已全部结束")

报告分类:

1.xlsx格式:

image-20240301105122180

创建一个“总”文件夹

脚本,1.xlsx,报告,“总”文件夹 放在同一文件夹下

image-20240327152233335

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import openpyxl
import os
import shutil

# 加载Excel文件
xlsx_file = '1.xlsx'
workbook = openpyxl.load_workbook(xlsx_file)
sheet = workbook.active

# 遍历每一行,根据部门和系统名称移动对应的docx文件
for row in sheet.iter_rows(min_row=2, values_only=True): # 从第二行开始读取数据
department = row[0]
system_name = row[1]

# 根据部门创建文件夹
department_folder = os.path.join('总', department)
os.makedirs(department_folder, exist_ok=True)

# 查找对应的docx文件
docx_file1 = f'未修复-{system_name}渗透测试复测报告.docx'
docx_file2 = f'已修复-{system_name}渗透测试复测报告.docx'
docx_file3 = f'高危-{system_name}渗透测试报告.docx'

if os.path.exists(docx_file1):
shutil.move(docx_file1, department_folder)
if os.path.exists(docx_file2):
shutil.move(docx_file2, department_folder)
if os.path.exists(docx_file3):
shutil.move(docx_file3, department_folder)

workbook.save(xlsx_file) # 保存Excel文件


Word批量化生成
http://syst1m.cn/2023/08/09/Word批量化生成/
作者
076w
发布于
2023年8月9日
许可协议