完成接口与接口文档
This commit is contained in:
parent
a6fa6ebf9a
commit
d71251be06
|
@ -197,6 +197,17 @@ async def generate_report(base_info, baogao_info):
|
|||
print(f"报告基本信息获取失败:{e}")
|
||||
return
|
||||
|
||||
#检查参数合法性
|
||||
if not if_fanglei or not if_neibu or not if_waibu:
|
||||
print("请至少选择一种检查项目")
|
||||
return
|
||||
if not os.path.exists(muban_dir):
|
||||
print(f"模板路径{muban_dir}不存在")
|
||||
return
|
||||
if not os.path.exists(shengcheng_dir):
|
||||
print(f"生成路径{shengcheng_dir}不存在")
|
||||
return
|
||||
|
||||
output_doc = None
|
||||
head_num = 1
|
||||
###封面创建###
|
||||
|
@ -215,279 +226,286 @@ async def generate_report(base_info, baogao_info):
|
|||
|
||||
mianzhe_shengming = f"本报告仅涵盖{'、'.join(baogao_label)}检测内容"
|
||||
|
||||
#创建文档、添加封面
|
||||
print(await create_document(output_dir))
|
||||
print(add_documents(output_dir, cover_dirs[0]))
|
||||
if check_pic_url(coverurl): #手动导入封面图片测试用
|
||||
print(await add_picture(output_dir, get_full_picture_url(coverurl), width = 6.41, height = 4))
|
||||
elif check_pic_url(cover_url):
|
||||
print(await add_picture(output_dir, get_full_picture_url(cover_url), width = 6.41, height = 4))
|
||||
else:
|
||||
print(await add_picture(output_dir, cover_dirs[1]))
|
||||
print(add_documents(output_dir, cover_dirs[2]))
|
||||
print("封面创建成功")
|
||||
#YYYY年MM月DD日 HH:MM:SS
|
||||
#更改文档信息
|
||||
print(await search_and_replace(output_dir, TITLE_OF_REPORT, jizu_bianhao))
|
||||
print(await search_and_replace(output_dir, baogao_name1, baogao_name))
|
||||
print(await search_and_replace(output_dir, baogao_name2, baogao_name))
|
||||
print(await search_and_replace(output_dir, company_name_yi, Yi_company))
|
||||
print(await search_and_replace(output_dir, cover_project, fengchang_name))
|
||||
print(await search_and_replace(output_dir, cover_encode, jizu_bianhao))
|
||||
print(await search_and_replace(output_dir, cover_date, date_year_month))
|
||||
print(await search_and_replace(output_dir, 'bianzhi', baogao_bianzhi))
|
||||
print(await search_and_replace(output_dir, 'shenghe', baogao_shenghe))
|
||||
print(await search_and_replace(output_dir, 'mianzhe_shengming', mianzhe_shengming))
|
||||
if baogao_info["if_docx_fengmian"] :
|
||||
#创建文档、添加封面
|
||||
print(await create_document(output_dir))
|
||||
print(add_documents(output_dir, cover_dirs[0]))
|
||||
if check_pic_url(coverurl): #手动导入封面图片测试用
|
||||
print(await add_picture(output_dir, get_full_picture_url(coverurl), width = 6.41, height = 4))
|
||||
elif check_pic_url(cover_url):
|
||||
print(await add_picture(output_dir, get_full_picture_url(cover_url), width = 6.41, height = 4))
|
||||
else:
|
||||
print(await add_picture(output_dir, cover_dirs[1]))
|
||||
print(add_documents(output_dir, cover_dirs[2]))
|
||||
print("封面创建成功")
|
||||
#YYYY年MM月DD日 HH:MM:SS
|
||||
#更改文档信息
|
||||
print(await search_and_replace(output_dir, TITLE_OF_REPORT, jizu_bianhao))
|
||||
print(await search_and_replace(output_dir, baogao_name1, baogao_name))
|
||||
print(await search_and_replace(output_dir, baogao_name2, baogao_name))
|
||||
print(await search_and_replace(output_dir, company_name_yi, Yi_company))
|
||||
print(await search_and_replace(output_dir, cover_project, fengchang_name))
|
||||
print(await search_and_replace(output_dir, cover_encode, jizu_bianhao))
|
||||
print(await search_and_replace(output_dir, cover_date, date_year_month))
|
||||
print(await search_and_replace(output_dir, 'bianzhi', baogao_bianzhi))
|
||||
print(await search_and_replace(output_dir, 'shenghe', baogao_shenghe))
|
||||
print(await search_and_replace(output_dir, 'mianzhe_shengming', mianzhe_shengming))
|
||||
|
||||
total_table_num = 0
|
||||
#项目概况表
|
||||
print("开始添加项目概况表")
|
||||
XIANG_MU_GAI_KUANG = os.path.join(muban_dir,"xiangmugaikuo.docx")
|
||||
print(f"查找模板,找到模板:{XIANG_MU_GAI_KUANG}")
|
||||
project_location = fengchang_location
|
||||
company_name_jia = Jia_company
|
||||
fuzeren = yi_fuzeren
|
||||
phone_fuzeren = yi_phone
|
||||
xiangmuguige = jizu_num
|
||||
Yi_company = Yi_company
|
||||
XIANGMU_GAIKUO = list(list("" for i in range(6)) for j in range(5))
|
||||
XIANGMU_GAIKUO[0][1] = fengchang_name
|
||||
XIANGMU_GAIKUO[0][4] = project_location
|
||||
XIANGMU_GAIKUO[1][1] = company_name_jia
|
||||
XIANGMU_GAIKUO[1][4] = Yi_company
|
||||
XIANGMU_GAIKUO[2][1] = jia_fuzeren
|
||||
XIANGMU_GAIKUO[2][4] = fuzeren
|
||||
XIANGMU_GAIKUO[3][2] = jia_phone
|
||||
XIANGMU_GAIKUO[3][5] = phone_fuzeren
|
||||
XIANGMU_GAIKUO[4][1] = jizu_xinghao
|
||||
XIANGMU_GAIKUO[4][3] = xiangmuguige
|
||||
XIANGMU_GAIKUO[4][5] = gongqi
|
||||
print("建立表结构完毕,开始插入模板")
|
||||
#添加项目概况表
|
||||
print(f"输出路径:{output_dir},模板路径:{XIANG_MU_GAI_KUANG},插入数据:{XIANGMU_GAIKUO}")
|
||||
output_doc, message = await add_table_to_document(output_dir, XIANG_MU_GAI_KUANG,5,5,total_table_num,XIANGMU_GAIKUO)
|
||||
print(message)
|
||||
print("模板插入完毕,开始替换内容")
|
||||
total_table_num += 1
|
||||
print(await search_and_replace(output_dir, jiegou_xuhao, num_to_chinese[head_num]))
|
||||
head_num += 1
|
||||
|
||||
#检查方案描述
|
||||
FANGAN_JIANCHA_DIR = os.path.join(muban_dir,"checkmethod.docx")
|
||||
list_to_replace = {
|
||||
'renyuan_peizhi' : "\n".join(renyuan_peizhi),
|
||||
'shebei_peizhi' : "\n".join(shebei_peizhi),
|
||||
'shigong_fangan' : "\n".join(shigong_fangan),
|
||||
'gongzuo_neirong' : "\n".join(gongzuo_neirong),
|
||||
'beizhu' : beizhu,
|
||||
'num' : num_to_chinese[head_num],
|
||||
}
|
||||
print(await add_table_and_replace(output_dir, FANGAN_JIANCHA_DIR, 0, list_to_replace))
|
||||
print(split_table_by_row_content(output_dir, output_dir, total_table_num))
|
||||
total_table_num += 1
|
||||
head_num += 1
|
||||
|
||||
|
||||
JIANCHA_XINGXI_DIR = os.path.join(muban_dir,"checkinfo.docx")
|
||||
JIANCHA_XINGXI = list(list("" for i in range(4)) for j in range(9))
|
||||
JIANCHA_XINGXI[0][1] = jiancha_renyuan
|
||||
try:
|
||||
JIANCHA_XINGXI[1][1] = Jiancha_date.split('T')[0]
|
||||
except:
|
||||
JIANCHA_XINGXI[1][1] = "格式不对或无数据"
|
||||
JIANCHA_XINGXI[1][3] = jizu_bianhao
|
||||
JIANCHA_XINGXI[2][1] = "风力发电机组" + baogao_name
|
||||
JIANCHA_XINGXI[2][3] = "、".join(jiancha)
|
||||
JIANCHA_XINGXI[3][2] = partManufacturer
|
||||
JIANCHA_XINGXI[4][1] = '叶片型号:' + jizu_xinghao
|
||||
JIANCHA_XINGXI[5][1] = Y_Code[0] if len(Y_Code) > 0 else "无"
|
||||
JIANCHA_XINGXI[6][1] = Y_Code[1] if len(Y_Code) > 1 else "无"
|
||||
JIANCHA_XINGXI[7][1] = Y_Code[2] if len(Y_Code) > 2 else "无"
|
||||
JIANCHA_XINGXI[8][0] = "本次" + "、".join(_ for _ in jiancha) + f"检查,采集叶片图片{total_picture_num}张,内容覆盖" + ";".join(_ for _ in neirong)
|
||||
#新建检查信息表
|
||||
output_doc, message = await add_table_to_document(output_dir, JIANCHA_XINGXI_DIR,9,4,total_table_num ,JIANCHA_XINGXI,False)
|
||||
print(message)
|
||||
print(await search_and_replace(output_dir, jiegou_xuhao, num_to_chinese[head_num]))
|
||||
head_num += 1
|
||||
total_table_num += 1
|
||||
|
||||
# 添加成果递交表
|
||||
CHENGGUO_DIJIAO_DIR = os.path.join(muban_dir,"chengguo_sub.docx")
|
||||
CHENGGUO_DIJIAO = list(list("" for i in range(4)) for j in range(6))
|
||||
CHENGGUO_DIJIAO[0][1] = jiancha_renyuan
|
||||
CHENGGUO_DIJIAO[1][1] = jia_fuzeren
|
||||
try:
|
||||
CHENGGUO_DIJIAO[2][1] = Jiancha_date.split('T')[0]
|
||||
except:
|
||||
CHENGGUO_DIJIAO[2][1] = "格式不对或无数据"
|
||||
CHENGGUO_DIJIAO[3][1] = data_processor
|
||||
CHENGGUO_DIJIAO[4][1] = baogao_bianzhi
|
||||
CHENGGUO_DIJIAO[5][1] = baogao_shenghe
|
||||
try:
|
||||
CHENGGUO_DIJIAO[2][3] = Jiancha_date.split('T')[1]
|
||||
except:
|
||||
CHENGGUO_DIJIAO[2][3] = "格式不对或无数据"
|
||||
CHENGGUO_DIJIAO[3][3] = baogao_date.split(' ')[0]
|
||||
CHENGGUO_DIJIAO[4][3] = baogao_date.split(' ')[0]
|
||||
CHENGGUO_DIJIAO[5][3] = "未审核"
|
||||
|
||||
output_doc, message = await add_table_to_document(output_dir, CHENGGUO_DIJIAO_DIR,5,5,total_table_num,CHENGGUO_DIJIAO,True,0.04)
|
||||
print(message)
|
||||
print(await search_and_replace(output_dir, jiegou_xuhao, num_to_chinese[head_num]))
|
||||
head_num += 1
|
||||
total_table_num += 1
|
||||
|
||||
#检查情况汇总表(文字信息)
|
||||
|
||||
try:
|
||||
#获取缺陷信息
|
||||
"""
|
||||
需要获取:
|
||||
Y1、Y2、Y3叶片的缺陷数量,缺陷字典{描述:图片路径}
|
||||
和数据库连接需要的新增异常处理:
|
||||
目前逻辑为找到图片后,找缺陷类型的图片,通过图片id查找对应缺陷记录
|
||||
如果没有找到,要返回对应异常,即已选出(标注)缺陷图,但未填写对应缺陷信息的异常返回。
|
||||
最后将无异常的图片入队缺陷字典(同数量),有异常的图片再另存。
|
||||
"""
|
||||
|
||||
Y1_quexian_list = []
|
||||
Y2_quexian_list = []
|
||||
Y3_quexian_list = []
|
||||
Y1_quexian_list = defect_records_with_pic["叶片1"]["DEFECT"] if "叶片1" in defect_records_with_pic else {}
|
||||
Y2_quexian_list = defect_records_with_pic["叶片2"]["DEFECT"] if "叶片2" in defect_records_with_pic else {}
|
||||
Y3_quexian_list = defect_records_with_pic["叶片3"]["DEFECT"] if "叶片3" in defect_records_with_pic else {}
|
||||
|
||||
Y1_quexian_num = len(Y1_quexian_list)
|
||||
Y2_quexian_num = len(Y2_quexian_list)
|
||||
Y3_quexian_num = len(Y3_quexian_list)
|
||||
no_defect_found = "未发现明显缺陷"
|
||||
weak_num_Y1 = f"{Y_Code[0] if len(Y_Code) > 0 else '无'}叶片" + f"共发现缺陷{Y1_quexian_num}处" if Y1_quexian_num > 0 else no_defect_found
|
||||
weak_num_Y2 = f"{Y_Code[1] if len(Y_Code) > 1 else '无'}叶片" + f"共发现缺陷{Y2_quexian_num}处" if Y1_quexian_num > 0 else no_defect_found
|
||||
weak_num_Y3 = f"{Y_Code[2] if len(Y_Code) > 2 else '无'}叶片" + f"共发现缺陷{Y3_quexian_num}处" if Y1_quexian_num > 0 else no_defect_found
|
||||
except Exception as e:
|
||||
print(f"缺陷图获取失败:{e}")
|
||||
return
|
||||
|
||||
#添加检查情况汇总表
|
||||
JIANCHA_HUIZONG_DIR = os.path.join(muban_dir,"total_check.docx")
|
||||
JIANCHA_HUIZONG = list(list("" for i in range(3)) for j in range(4))
|
||||
|
||||
JIANCHA_HUIZONG[1][0] = weak_num_Y1
|
||||
JIANCHA_HUIZONG[2][0] = weak_num_Y2
|
||||
JIANCHA_HUIZONG[3][0] = weak_num_Y3
|
||||
JIANCHA_HUIZONG[1][1] = "\n".join(neirong2)
|
||||
JIANCHA_HUIZONG[2][1] = "\n".join(neirong2)
|
||||
JIANCHA_HUIZONG[3][1] = "\n".join(neirong2)
|
||||
JIANCHA_HUIZONG[1][2] = "\n".join([f"{i+1}.{s}" for i, s in enumerate(get_defect_str(Y1_quexian_list))]) if Y1_quexian_num else '未发现明显影响风力发电机组正常运行的缺陷'
|
||||
JIANCHA_HUIZONG[2][2] = "\n".join([f"{i+1}.{s}" for i, s in enumerate(get_defect_str(Y2_quexian_list))]) if Y2_quexian_num else '未发现明显影响风力发电机组正常运行的缺陷'
|
||||
JIANCHA_HUIZONG[3][2] = "\n".join([f"{i+1}.{s}" for i, s in enumerate(get_defect_str(Y3_quexian_list))]) if Y3_quexian_num else '未发现明显影响风力发电机组正常运行的缺陷'
|
||||
output_doc, message = await add_table_to_document(output_dir, JIANCHA_HUIZONG_DIR,4,3,total_table_num,JIANCHA_HUIZONG,False,ALIGMENT='LEFT')
|
||||
print(message)
|
||||
print(await search_and_replace(output_dir, jiegou_xuhao, num_to_chinese[head_num]))
|
||||
total_table_num += 1
|
||||
head_num += 1
|
||||
|
||||
#主要部位图片展示表/检查内容表
|
||||
#获取典型图信息
|
||||
search_file_list = ["外汇总","内汇总","防汇总"]
|
||||
try:
|
||||
Y1_list = safe_get(filtered_picture_data, "叶片1", dianxing_type, default=[])
|
||||
Y2_list = safe_get(filtered_picture_data, "叶片2", dianxing_type, default=[])
|
||||
Y3_list = safe_get(filtered_picture_data, "叶片3", dianxing_type, default=[])
|
||||
picture_Y1_num = len(Y1_list)
|
||||
picture_Y2_num = len(Y2_list)
|
||||
picture_Y3_num = len(Y3_list)
|
||||
except Exception as e:
|
||||
print(f"获取缺陷图失败:{e}")
|
||||
print(f"图片、文字数量:{picture_Y1_num} {picture_Y2_num} {picture_Y3_num}")
|
||||
JIANCHA_NEIRONG_TOTAL_NUM = picture_Y1_num+ picture_Y2_num + picture_Y3_num
|
||||
if JIANCHA_NEIRONG_TOTAL_NUM <= 0:
|
||||
print("无典型图片数据,不生成典型图表")
|
||||
else:
|
||||
col ,row = 3, 0
|
||||
JIANCHA_NEIRONG_PICTURES_TABLE = os.path.join(muban_dir,"check2.docx")
|
||||
JIANCHA_NEIRONG_Y1_DIR = os.path.join(muban_dir,"check_content.docx")
|
||||
JIANCHA_NEIRONG_Y1 = list(list("" for _ in range(3)) for j in range(1))
|
||||
Y1_code = Y_Code[0] if len(Y_Code) > 0 else "无"
|
||||
Y2_code = Y_Code[1] if len(Y_Code) > 1 else "无"
|
||||
Y3_code = Y_Code[2] if len(Y_Code) > 2 else "无"
|
||||
JIANCHA_NEIRONG_Y1[0][0] = f"叶片1:{Y1_code}检查内容"
|
||||
print(f"Y1标题内容:{JIANCHA_NEIRONG_Y1}")
|
||||
JIANCHA_NEIRONG_Y2_DIR = os.path.join(muban_dir,"check3.docx")
|
||||
JIANCHA_NEIRONG_Y2 = list(list("" for _ in range(3)) for j in range(1))
|
||||
JIANCHA_NEIRONG_Y2[0][0] = f"叶片2:{Y2_code}检查内容"
|
||||
print(f"Y2标题内容:{JIANCHA_NEIRONG_Y2}")
|
||||
JIANCHA_NEIRONG_Y3_DIR = os.path.join(muban_dir,"check3.docx")
|
||||
JIANCHA_NEIRONG_Y3 = list(list("" for _ in range(3)) for j in range(1))
|
||||
JIANCHA_NEIRONG_Y3[0][0] = f"叶片3:{Y3_code}检查内容"
|
||||
print(f"Y3标题内容:{JIANCHA_NEIRONG_Y3}")
|
||||
print(f"当前表格序号为 {total_table_num}")
|
||||
print(key_words)
|
||||
output_doc, message = await add_table_to_document(output_dir, JIANCHA_NEIRONG_Y1_DIR,1,3,total_table_num,JIANCHA_NEIRONG_Y1,True, 1)
|
||||
if baogao_info["if_docx_project_overview"]:
|
||||
#项目概况表
|
||||
print("开始添加项目概况表")
|
||||
XIANG_MU_GAI_KUANG = os.path.join(muban_dir,"xiangmugaikuo.docx")
|
||||
print(f"查找模板,找到模板:{XIANG_MU_GAI_KUANG}")
|
||||
project_location = fengchang_location
|
||||
company_name_jia = Jia_company
|
||||
fuzeren = yi_fuzeren
|
||||
phone_fuzeren = yi_phone
|
||||
xiangmuguige = jizu_num
|
||||
Yi_company = Yi_company
|
||||
XIANGMU_GAIKUO = list(list("" for i in range(6)) for j in range(5))
|
||||
XIANGMU_GAIKUO[0][1] = fengchang_name
|
||||
XIANGMU_GAIKUO[0][4] = project_location
|
||||
XIANGMU_GAIKUO[1][1] = company_name_jia
|
||||
XIANGMU_GAIKUO[1][4] = Yi_company
|
||||
XIANGMU_GAIKUO[2][1] = jia_fuzeren
|
||||
XIANGMU_GAIKUO[2][4] = fuzeren
|
||||
XIANGMU_GAIKUO[3][2] = jia_phone
|
||||
XIANGMU_GAIKUO[3][5] = phone_fuzeren
|
||||
XIANGMU_GAIKUO[4][1] = jizu_xinghao
|
||||
XIANGMU_GAIKUO[4][3] = xiangmuguige
|
||||
XIANGMU_GAIKUO[4][5] = gongqi
|
||||
print("建立表结构完毕,开始插入模板")
|
||||
#添加项目概况表
|
||||
print(f"输出路径:{output_dir},模板路径:{XIANG_MU_GAI_KUANG},插入数据:{XIANGMU_GAIKUO}")
|
||||
output_doc, message = await add_table_to_document(output_dir, XIANG_MU_GAI_KUANG,5,5,total_table_num,XIANGMU_GAIKUO)
|
||||
print(message)
|
||||
total_table_num += 1
|
||||
|
||||
total_table_num = await process_server_images_table(Y1_list, image_source_to_find, output_dir, total_table_num, JIANCHA_NEIRONG_PICTURES_TABLE, key_words)
|
||||
|
||||
output_doc, message = await add_table_to_document(output_dir, JIANCHA_NEIRONG_Y2_DIR,1,3,total_table_num,JIANCHA_NEIRONG_Y2,True, 1)
|
||||
print(message)
|
||||
total_table_num += 1
|
||||
|
||||
total_table_num = await process_server_images_table(Y2_list, output_dir, total_table_num, JIANCHA_NEIRONG_PICTURES_TABLE, key_words)
|
||||
|
||||
output_doc, message = await add_table_to_document(output_dir, JIANCHA_NEIRONG_Y3_DIR,1,3,total_table_num,JIANCHA_NEIRONG_Y3,True, 1)
|
||||
print(message)
|
||||
total_table_num += 1
|
||||
|
||||
total_table_num = await process_server_images_table(Y3_list, output_dir, total_table_num, JIANCHA_NEIRONG_PICTURES_TABLE, key_words)
|
||||
print("模板插入完毕,开始替换内容")
|
||||
total_table_num += 1
|
||||
print(await search_and_replace(output_dir, jiegou_xuhao, num_to_chinese[head_num]))
|
||||
head_num += 1
|
||||
|
||||
# #缺陷详情
|
||||
QUEXIAN_XIANGQING_DIR = os.path.join(muban_dir,"check_check.docx")
|
||||
QUEXIAN_XIANGQING_TITLE_DIR = os.path.join(muban_dir,"check_check_title.docx")
|
||||
if baogao_info['if_docx_inspection_method']:
|
||||
#检查方案描述
|
||||
FANGAN_JIANCHA_DIR = os.path.join(muban_dir,"checkmethod.docx")
|
||||
list_to_replace = {
|
||||
'renyuan_peizhi' : "\n".join(renyuan_peizhi),
|
||||
'shebei_peizhi' : "\n".join(shebei_peizhi),
|
||||
'shigong_fangan' : "\n".join(shigong_fangan),
|
||||
'gongzuo_neirong' : "\n".join(gongzuo_neirong),
|
||||
'beizhu' : beizhu,
|
||||
'num' : num_to_chinese[head_num],
|
||||
}
|
||||
print(await add_table_and_replace(output_dir, FANGAN_JIANCHA_DIR, 0, list_to_replace))
|
||||
print(split_table_by_row_content(output_dir, output_dir, total_table_num))
|
||||
total_table_num += 1
|
||||
head_num += 1
|
||||
|
||||
if baogao_info['if_docx_inspection_info']:
|
||||
#检查信息
|
||||
JIANCHA_XINGXI_DIR = os.path.join(muban_dir,"checkinfo.docx")
|
||||
JIANCHA_XINGXI = list(list("" for i in range(4)) for j in range(9))
|
||||
JIANCHA_XINGXI[0][1] = jiancha_renyuan
|
||||
try:
|
||||
JIANCHA_XINGXI[1][1] = Jiancha_date.split('T')[0]
|
||||
except:
|
||||
JIANCHA_XINGXI[1][1] = "格式不对或无数据"
|
||||
JIANCHA_XINGXI[1][3] = jizu_bianhao
|
||||
JIANCHA_XINGXI[2][1] = "风力发电机组" + baogao_name
|
||||
JIANCHA_XINGXI[2][3] = "、".join(jiancha)
|
||||
JIANCHA_XINGXI[3][2] = partManufacturer
|
||||
JIANCHA_XINGXI[4][1] = '叶片型号:' + jizu_xinghao
|
||||
JIANCHA_XINGXI[5][1] = Y_Code[0] if len(Y_Code) > 0 else "无"
|
||||
JIANCHA_XINGXI[6][1] = Y_Code[1] if len(Y_Code) > 1 else "无"
|
||||
JIANCHA_XINGXI[7][1] = Y_Code[2] if len(Y_Code) > 2 else "无"
|
||||
JIANCHA_XINGXI[8][0] = "本次" + "、".join(_ for _ in jiancha) + f"检查,采集叶片图片{total_picture_num}张,内容覆盖" + ";".join(_ for _ in neirong)
|
||||
#新建检查信息表
|
||||
output_doc, message = await add_table_to_document(output_dir, JIANCHA_XINGXI_DIR,9,4,total_table_num ,JIANCHA_XINGXI,False)
|
||||
print(message)
|
||||
print(await search_and_replace(output_dir, jiegou_xuhao, num_to_chinese[head_num]))
|
||||
head_num += 1
|
||||
total_table_num += 1
|
||||
|
||||
if baogao_info['if_docx_chengguo_sub']:
|
||||
# 添加成果递交表
|
||||
CHENGGUO_DIJIAO_DIR = os.path.join(muban_dir,"chengguo_sub.docx")
|
||||
CHENGGUO_DIJIAO = list(list("" for i in range(4)) for j in range(6))
|
||||
CHENGGUO_DIJIAO[0][1] = jiancha_renyuan
|
||||
CHENGGUO_DIJIAO[1][1] = jia_fuzeren
|
||||
try:
|
||||
CHENGGUO_DIJIAO[2][1] = Jiancha_date.split('T')[0]
|
||||
except:
|
||||
CHENGGUO_DIJIAO[2][1] = "格式不对或无数据"
|
||||
CHENGGUO_DIJIAO[3][1] = data_processor
|
||||
CHENGGUO_DIJIAO[4][1] = baogao_bianzhi
|
||||
CHENGGUO_DIJIAO[5][1] = baogao_shenghe
|
||||
try:
|
||||
CHENGGUO_DIJIAO[2][3] = Jiancha_date.split('T')[1]
|
||||
except:
|
||||
CHENGGUO_DIJIAO[2][3] = "格式不对或无数据"
|
||||
CHENGGUO_DIJIAO[3][3] = baogao_date.split(' ')[0]
|
||||
CHENGGUO_DIJIAO[4][3] = baogao_date.split(' ')[0]
|
||||
CHENGGUO_DIJIAO[5][3] = "未审核"
|
||||
|
||||
output_doc, message = await add_table_to_document(output_dir, CHENGGUO_DIJIAO_DIR,5,5,total_table_num,CHENGGUO_DIJIAO,True,0.04)
|
||||
print(message)
|
||||
print(await search_and_replace(output_dir, jiegou_xuhao, num_to_chinese[head_num]))
|
||||
head_num += 1
|
||||
total_table_num += 1
|
||||
|
||||
if baogao_info['if_docx_inspection_text'] and if_waibu:
|
||||
#检查情况汇总表(文字信息)
|
||||
|
||||
try:
|
||||
#获取缺陷信息
|
||||
"""
|
||||
需要获取:
|
||||
Y1、Y2、Y3叶片的缺陷数量,缺陷字典{描述:图片路径}
|
||||
和数据库连接需要的新增异常处理:
|
||||
目前逻辑为找到图片后,找缺陷类型的图片,通过图片id查找对应缺陷记录
|
||||
如果没有找到,要返回对应异常,即已选出(标注)缺陷图,但未填写对应缺陷信息的异常返回。
|
||||
最后将无异常的图片入队缺陷字典(同数量),有异常的图片再另存。
|
||||
"""
|
||||
|
||||
Y1_quexian_list = []
|
||||
Y2_quexian_list = []
|
||||
Y3_quexian_list = []
|
||||
Y1_quexian_list = defect_records_with_pic["叶片1"]["DEFECT"] if "叶片1" in defect_records_with_pic else {}
|
||||
Y2_quexian_list = defect_records_with_pic["叶片2"]["DEFECT"] if "叶片2" in defect_records_with_pic else {}
|
||||
Y3_quexian_list = defect_records_with_pic["叶片3"]["DEFECT"] if "叶片3" in defect_records_with_pic else {}
|
||||
|
||||
Y1_quexian_num = len(Y1_quexian_list)
|
||||
Y2_quexian_num = len(Y2_quexian_list)
|
||||
Y3_quexian_num = len(Y3_quexian_list)
|
||||
no_defect_found = "未发现明显缺陷"
|
||||
weak_num_Y1 = f"{Y_Code[0] if len(Y_Code) > 0 else '无'}叶片" + f"共发现缺陷{Y1_quexian_num}处" if Y1_quexian_num > 0 else no_defect_found
|
||||
weak_num_Y2 = f"{Y_Code[1] if len(Y_Code) > 1 else '无'}叶片" + f"共发现缺陷{Y2_quexian_num}处" if Y1_quexian_num > 0 else no_defect_found
|
||||
weak_num_Y3 = f"{Y_Code[2] if len(Y_Code) > 2 else '无'}叶片" + f"共发现缺陷{Y3_quexian_num}处" if Y1_quexian_num > 0 else no_defect_found
|
||||
except Exception as e:
|
||||
print(f"缺陷图获取失败:{e}")
|
||||
return
|
||||
|
||||
Y_tables = [Y1_quexian_list,Y2_quexian_list,Y3_quexian_list]
|
||||
Y1_table_list = []
|
||||
Y2_table_list = []
|
||||
Y3_table_list = []
|
||||
table_lists = [Y1_table_list, Y2_table_list, Y3_table_list]
|
||||
#添加检查情况汇总表
|
||||
JIANCHA_HUIZONG_DIR = os.path.join(muban_dir,"total_check.docx")
|
||||
JIANCHA_HUIZONG = list(list("" for i in range(3)) for j in range(4))
|
||||
|
||||
for i, (table_list, Y_lists) in enumerate(zip(table_lists, Y_tables)):
|
||||
for Y_list in Y_lists:
|
||||
image_defect_record = Y_list["record"]
|
||||
image_path = Y_list["imagePath"]
|
||||
JIANCHA_HUIZONG[1][0] = weak_num_Y1
|
||||
JIANCHA_HUIZONG[2][0] = weak_num_Y2
|
||||
JIANCHA_HUIZONG[3][0] = weak_num_Y3
|
||||
JIANCHA_HUIZONG[1][1] = "\n".join(neirong2)
|
||||
JIANCHA_HUIZONG[2][1] = "\n".join(neirong2)
|
||||
JIANCHA_HUIZONG[3][1] = "\n".join(neirong2)
|
||||
JIANCHA_HUIZONG[1][2] = "\n".join([f"{i+1}.{s}" for i, s in enumerate(get_defect_str(Y1_quexian_list))]) if Y1_quexian_num else '未发现明显影响风力发电机组正常运行的缺陷'
|
||||
JIANCHA_HUIZONG[2][2] = "\n".join([f"{i+1}.{s}" for i, s in enumerate(get_defect_str(Y2_quexian_list))]) if Y2_quexian_num else '未发现明显影响风力发电机组正常运行的缺陷'
|
||||
JIANCHA_HUIZONG[3][2] = "\n".join([f"{i+1}.{s}" for i, s in enumerate(get_defect_str(Y3_quexian_list))]) if Y3_quexian_num else '未发现明显影响风力发电机组正常运行的缺陷'
|
||||
output_doc, message = await add_table_to_document(output_dir, JIANCHA_HUIZONG_DIR,4,3,total_table_num,JIANCHA_HUIZONG,False,ALIGMENT='LEFT')
|
||||
print(message)
|
||||
print(await search_and_replace(output_dir, jiegou_xuhao, num_to_chinese[head_num]))
|
||||
total_table_num += 1
|
||||
head_num += 1
|
||||
|
||||
if baogao_info['if_docx_inspection_picture']:
|
||||
#主要部位图片展示表/检查内容表
|
||||
#获取典型图信息
|
||||
try:
|
||||
Y1_list = safe_get(filtered_picture_data, "叶片1", dianxing_type, default=[])
|
||||
Y2_list = safe_get(filtered_picture_data, "叶片2", dianxing_type, default=[])
|
||||
Y3_list = safe_get(filtered_picture_data, "叶片3", dianxing_type, default=[])
|
||||
picture_Y1_num = len(Y1_list)
|
||||
picture_Y2_num = len(Y2_list)
|
||||
picture_Y3_num = len(Y3_list)
|
||||
except Exception as e:
|
||||
print(f"获取缺陷图失败:{e}")
|
||||
print(f"图片、文字数量:{picture_Y1_num} {picture_Y2_num} {picture_Y3_num}")
|
||||
JIANCHA_NEIRONG_TOTAL_NUM = picture_Y1_num+ picture_Y2_num + picture_Y3_num
|
||||
if JIANCHA_NEIRONG_TOTAL_NUM <= 0:
|
||||
print("无典型图片数据,不生成典型图表")
|
||||
else:
|
||||
col ,row = 3, 0
|
||||
JIANCHA_NEIRONG_PICTURES_TABLE = os.path.join(muban_dir,"check2.docx")
|
||||
JIANCHA_NEIRONG_Y1_DIR = os.path.join(muban_dir,"check_content.docx")
|
||||
JIANCHA_NEIRONG_Y1 = list(list("" for _ in range(3)) for j in range(1))
|
||||
Y1_code = Y_Code[0] if len(Y_Code) > 0 else "无"
|
||||
Y2_code = Y_Code[1] if len(Y_Code) > 1 else "无"
|
||||
Y3_code = Y_Code[2] if len(Y_Code) > 2 else "无"
|
||||
JIANCHA_NEIRONG_Y1[0][0] = f"叶片1:{Y1_code}检查内容"
|
||||
print(f"Y1标题内容:{JIANCHA_NEIRONG_Y1}")
|
||||
JIANCHA_NEIRONG_Y2_DIR = os.path.join(muban_dir,"check3.docx")
|
||||
JIANCHA_NEIRONG_Y2 = list(list("" for _ in range(3)) for j in range(1))
|
||||
JIANCHA_NEIRONG_Y2[0][0] = f"叶片2:{Y2_code}检查内容"
|
||||
print(f"Y2标题内容:{JIANCHA_NEIRONG_Y2}")
|
||||
JIANCHA_NEIRONG_Y3_DIR = os.path.join(muban_dir,"check3.docx")
|
||||
JIANCHA_NEIRONG_Y3 = list(list("" for _ in range(3)) for j in range(1))
|
||||
JIANCHA_NEIRONG_Y3[0][0] = f"叶片3:{Y3_code}检查内容"
|
||||
print(f"Y3标题内容:{JIANCHA_NEIRONG_Y3}")
|
||||
print(f"当前表格序号为 {total_table_num}")
|
||||
print(key_words)
|
||||
output_doc, message = await add_table_to_document(output_dir, JIANCHA_NEIRONG_Y1_DIR,1,3,total_table_num,JIANCHA_NEIRONG_Y1,True, 1)
|
||||
print(message)
|
||||
total_table_num += 1
|
||||
|
||||
total_table_num = await process_server_images_table(Y1_list, image_source_to_find, output_dir, total_table_num, JIANCHA_NEIRONG_PICTURES_TABLE, key_words)
|
||||
|
||||
output_doc, message = await add_table_to_document(output_dir, JIANCHA_NEIRONG_Y2_DIR,1,3,total_table_num,JIANCHA_NEIRONG_Y2,True, 1)
|
||||
print(message)
|
||||
total_table_num += 1
|
||||
|
||||
total_table_num = await process_server_images_table(Y2_list, output_dir, total_table_num, JIANCHA_NEIRONG_PICTURES_TABLE, key_words)
|
||||
|
||||
output_doc, message = await add_table_to_document(output_dir, JIANCHA_NEIRONG_Y3_DIR,1,3,total_table_num,JIANCHA_NEIRONG_Y3,True, 1)
|
||||
print(message)
|
||||
total_table_num += 1
|
||||
|
||||
total_table_num = await process_server_images_table(Y3_list, output_dir, total_table_num, JIANCHA_NEIRONG_PICTURES_TABLE, key_words)
|
||||
print(await search_and_replace(output_dir, jiegou_xuhao, num_to_chinese[head_num]))
|
||||
head_num += 1
|
||||
|
||||
image_defect_record = get_defect_detail(image_defect_record["defectId"])
|
||||
if baogao_info['if_docx_defect_picture'] and if_waibu:
|
||||
# #缺陷详情
|
||||
QUEXIAN_XIANGQING_DIR = os.path.join(muban_dir,"check_check.docx")
|
||||
QUEXIAN_XIANGQING_TITLE_DIR = os.path.join(muban_dir,"check_check_title.docx")
|
||||
|
||||
Y_tables = [Y1_quexian_list,Y2_quexian_list,Y3_quexian_list]
|
||||
Y1_table_list = []
|
||||
Y2_table_list = []
|
||||
Y3_table_list = []
|
||||
table_lists = [Y1_table_list, Y2_table_list, Y3_table_list]
|
||||
|
||||
# 从imagedefectrecord中获取各个字段
|
||||
defect_type = image_defect_record.get('defectTypeLabel', '未知缺陷类型')
|
||||
defect_location = f"{image_defect_record.get('partName', '')}{image_defect_record.get('defectPosition', '')}"
|
||||
|
||||
# 使用axial和chordwise作为缺陷尺寸信息
|
||||
axial = image_defect_record.get('axial', '')
|
||||
chordwise = image_defect_record.get('chordwise', '')
|
||||
defect_size = f"轴向:{axial} 弦向:{chordwise}" if axial or chordwise else ''
|
||||
|
||||
visibility = "暂无此项"
|
||||
|
||||
urgency = "暂无此项"
|
||||
|
||||
severity = image_defect_record.get('defectLevelLabel', '未知严重等级')
|
||||
repair_suggestion = image_defect_record.get('repairIdea', '无维修建议')
|
||||
|
||||
print(f"获取第{i+1}个叶片的缺陷图: {image_path}")
|
||||
|
||||
table_list.append({
|
||||
"QueXianLeiXing": defect_type,
|
||||
"QueXianWeiZhi": defect_location,
|
||||
"QueXianChiCun": defect_size,
|
||||
"WeiZongDengJi": severity,
|
||||
"Tupian_Dir": get_full_picture_url(image_path) if check_pic_url(image_path) else None,
|
||||
"visibility": visibility,
|
||||
"urgency": urgency,
|
||||
"repair_suggestion": repair_suggestion, # 新增维修建议字段
|
||||
})
|
||||
for i, (table_list, Y_lists) in enumerate(zip(table_lists, Y_tables)):
|
||||
for Y_list in Y_lists:
|
||||
image_defect_record = Y_list["record"]
|
||||
image_path = Y_list["imagePath"]
|
||||
|
||||
image_defect_record = get_defect_detail(image_defect_record["defectId"])
|
||||
|
||||
# 从imagedefectrecord中获取各个字段
|
||||
defect_type = image_defect_record.get('defectTypeLabel', '未知缺陷类型')
|
||||
defect_location = f"{image_defect_record.get('partName', '')}{image_defect_record.get('defectPosition', '')}"
|
||||
|
||||
# 使用axial和chordwise作为缺陷尺寸信息
|
||||
axial = image_defect_record.get('axial', '')
|
||||
chordwise = image_defect_record.get('chordwise', '')
|
||||
defect_size = f"轴向:{axial} 弦向:{chordwise}" if axial or chordwise else ''
|
||||
|
||||
visibility = "暂无此项"
|
||||
|
||||
urgency = "暂无此项"
|
||||
|
||||
severity = image_defect_record.get('defectLevelLabel', '未知严重等级')
|
||||
repair_suggestion = image_defect_record.get('repairIdea', '无维修建议')
|
||||
|
||||
print(f"获取第{i+1}个叶片的缺陷图: {image_path}")
|
||||
|
||||
table_list.append({
|
||||
"QueXianLeiXing": defect_type,
|
||||
"QueXianWeiZhi": defect_location,
|
||||
"QueXianChiCun": defect_size,
|
||||
"WeiZongDengJi": severity,
|
||||
"Tupian_Dir": get_full_picture_url(image_path) if check_pic_url(image_path) else None,
|
||||
"visibility": visibility,
|
||||
"urgency": urgency,
|
||||
"repair_suggestion": repair_suggestion, # 新增维修建议字段
|
||||
})
|
||||
|
||||
# for i, (table_list, Y_dict) in enumerate(zip(table_lists, Y_tables)):
|
||||
# for image_defect_record, image_path in Y_dict.items():
|
||||
|
@ -526,139 +544,57 @@ async def generate_report(base_info, baogao_info):
|
|||
# "repair_suggestion": "图片命名有误", # 新增维修建议字段
|
||||
# })
|
||||
|
||||
|
||||
Y1_TABLES, Y1_TABLES_PICTURES = fill_tables(table_lists[0],4,5,len(table_lists[0]),Y_Code[0] if len(Y_Code) > 0 else "无")
|
||||
Y2_TABLES, Y2_TABLES_PICTURES = fill_tables(table_lists[1],4,5,len(table_lists[1]),Y_Code[1] if len(Y_Code) > 1 else "无")
|
||||
Y3_TABLES, Y3_TABLES_PICTURES = fill_tables(table_lists[2],4,5,len(table_lists[2]),Y_Code[2] if len(Y_Code) > 2 else "无")
|
||||
print(add_documents(output_dir, QUEXIAN_XIANGQING_TITLE_DIR))
|
||||
print(await search_and_replace(output_dir, jiegou_xuhao, num_to_chinese[head_num]))
|
||||
head_num += 1
|
||||
table_num = 0
|
||||
Xu_Hao = 0
|
||||
total_table_num,table_num,Xu_Hao = await add_dynamic_table(output_doc,output_dir,table_num,Y1_TABLES,QUEXIAN_XIANGQING_DIR,Y1_TABLES_PICTURES,4,5,total_table_num,False,xuhao=Xu_Hao)
|
||||
total_table_num,table_num,Xu_Hao = await add_dynamic_table(output_doc,output_dir,table_num,Y2_TABLES,QUEXIAN_XIANGQING_DIR,Y2_TABLES_PICTURES,4,5,total_table_num,False,xuhao=Xu_Hao)
|
||||
total_table_num,table_num,Xu_Hao = await add_dynamic_table(output_doc,output_dir,table_num,Y3_TABLES,QUEXIAN_XIANGQING_DIR,Y3_TABLES_PICTURES,4,5,total_table_num,False,xuhao=Xu_Hao)
|
||||
|
||||
|
||||
#总结
|
||||
ZONG_JIE_DIR = os.path.join(muban_dir,"result.docx")
|
||||
ZONG_JIE_BEFORE = "result"
|
||||
ZONG_JIE = baogao_info['baogao_zongjie']
|
||||
print(add_documents(output_dir, ZONG_JIE_DIR))
|
||||
print(await search_and_replace(output_dir, ZONG_JIE_BEFORE, ZONG_JIE))
|
||||
print(await search_and_replace(output_dir, 'company_yi', Yi_company))
|
||||
print(await search_and_replace(output_dir, 'baogao_date', baogao_date.split(' ')[0]))
|
||||
print(await search_and_replace(output_dir, jiegou_xuhao, num_to_chinese[head_num]))
|
||||
|
||||
import argparse
|
||||
|
||||
def parse_arguments():
|
||||
parser = argparse.ArgumentParser(description='报告生成工具')
|
||||
|
||||
# 设置默认值
|
||||
default_json1 = {
|
||||
"turbine_id": "183463dbf40d9278549a76b82b175dd9",
|
||||
}
|
||||
default_json2 = {
|
||||
'shengcheng_dir': r"",
|
||||
'muban_dir': r"",
|
||||
"dianxing_enum" : "TYPICAL", # 典型图的枚举值
|
||||
"quexian_enum" : "DEFECT", # 缺陷图的枚举值
|
||||
"other_enum" : "OTHER", # 其他图的枚举值
|
||||
"waibu_enum" : "out-work",
|
||||
"neibu_enum" : "in-work",
|
||||
"fanglei_enum" : "lightning-protection-work",
|
||||
"if_waibu" : True,
|
||||
"if_neibu" : True,
|
||||
"if_fanglei" : True,
|
||||
"userName" : "admin",
|
||||
"baogaoCheck" : "未审核",
|
||||
'key_words': '缺,损,裂,脱,污', #关键字,用于汇总图的名字包含缺陷时标红,匹配逻辑为正则匹配单个字则为红 后续可优化
|
||||
|
||||
"data_processor" : "未获取",
|
||||
"shigong_fangan" : None,
|
||||
'jiancha_renyuan': '未获取',
|
||||
"check_date" : None,
|
||||
"coverurl" : None,
|
||||
"conclusion" : "未填写总结",
|
||||
}
|
||||
|
||||
parser.add_argument('--turbine_id', '--id', dest='turbine_id', default=default_json1['turbine_id'],
|
||||
help='''风机ID,默认为测试项目:"183463dbf40d9278549a76b82b175dd9"
|
||||
用于获取机组关联的所有静态信息与图片''')
|
||||
parser.add_argument('--output_dir', '--out', dest='output_dir', default=default_json2['shengcheng_dir'],
|
||||
help='输出目录,必填项')
|
||||
parser.add_argument('--template_dir', '--tpl', dest='template_dir', default=default_json2['muban_dir'],
|
||||
help='模板目录,必填项,用于获取模板文件,模板文件在同目录下的muban文件夹')
|
||||
parser.add_argument('--data_processor', '--dp', dest='data_processor', default=default_json2['data_processor'],
|
||||
help='数据处理人')
|
||||
parser.add_argument('--jiancha_renyuan', '--jc', dest='jiancha_renyuan', default=default_json2['jiancha_renyuan'],
|
||||
help='检查人')
|
||||
parser.add_argument('--check_date', '--cdate', dest='check_date', default=default_json2['check_date'],
|
||||
help='检查日期,格式为YYYY-MM-DD HH:MM:SS')
|
||||
parser.add_argument('--username', '--user', dest='username', default=default_json2['userName'],
|
||||
help='报告生成人,默认为admin')
|
||||
parser.add_argument('--if_waibu', '--waibu', dest='if_waibu', default=default_json2['if_waibu'],
|
||||
help='报告是否包含外部检测,默认为True')
|
||||
parser.add_argument('--if_neibu', '--neibu', dest='if_neibu', default=default_json2['if_neibu'],
|
||||
help='报告是否包含内部检测,默认为True')
|
||||
parser.add_argument('--if_fanglei', '--fanglei', dest='if_fanglei', default=default_json2['if_fanglei'],
|
||||
help='报告是否包含防雷检测,默认为True')
|
||||
parser.add_argument('--reportchecker', '--rc', dest='baogaoCheck', default=default_json2['baogaoCheck'],
|
||||
help='报告审核状态,默认为未审核')
|
||||
parser.add_argument('--conclusion', '--con', dest='conclusion', default=default_json2['conclusion'],
|
||||
help='总结,默认为未填写总结')
|
||||
parser.add_argument('--keywords', '--kw', dest='keywords', default=default_json2['key_words'],
|
||||
help='关键字,用于汇总图的名字包含缺陷时标红,匹配逻辑为正则匹配单个字则为红 后续可优化')
|
||||
parser.add_argument('--coverurl', default=default_json2['coverurl'], help='封面图片url,指定coverurl则不使用模板中的封面图片')
|
||||
parser.add_argument('--dianxing_enum', '--dx', dest='dianxing_enum', default=default_json2['dianxing_enum'],
|
||||
help='典型图的枚举值,默认为TYPICAL')
|
||||
parser.add_argument('--quexian_enum', '--qx', dest='quexian_enum', default=default_json2['quexian_enum'],
|
||||
help='缺陷图的枚举值,默认为DEFECT')
|
||||
parser.add_argument('--other_enum', '--other', dest='other_enum', default=default_json2['other_enum'],
|
||||
help='其他图的枚举值,默认为OTHER')
|
||||
parser.add_argument('--waibu_enum', '--wb', dest='waibu_enum', default=default_json2['waibu_enum'],
|
||||
help='外部检测的枚举值,默认为out_work')
|
||||
parser.add_argument('--neibu_enum', '--nb', dest='neibu_enum', default=default_json2['neibu_enum'],
|
||||
help='内部检测的枚举值,默认为in_work')
|
||||
parser.add_argument('--fanglei_enum', '--fb', dest='fanglei_enum', default=default_json2['fanglei_enum'],
|
||||
help='防雷检测的枚举值,默认为lightning-protection-work')
|
||||
return parser.parse_args()
|
||||
Y1_TABLES, Y1_TABLES_PICTURES = fill_tables(table_lists[0],4,5,len(table_lists[0]),Y_Code[0] if len(Y_Code) > 0 else "无")
|
||||
Y2_TABLES, Y2_TABLES_PICTURES = fill_tables(table_lists[1],4,5,len(table_lists[1]),Y_Code[1] if len(Y_Code) > 1 else "无")
|
||||
Y3_TABLES, Y3_TABLES_PICTURES = fill_tables(table_lists[2],4,5,len(table_lists[2]),Y_Code[2] if len(Y_Code) > 2 else "无")
|
||||
print(add_documents(output_dir, QUEXIAN_XIANGQING_TITLE_DIR))
|
||||
print(await search_and_replace(output_dir, jiegou_xuhao, num_to_chinese[head_num]))
|
||||
head_num += 1
|
||||
table_num = 0
|
||||
Xu_Hao = 0
|
||||
total_table_num,table_num,Xu_Hao = await add_dynamic_table(output_doc,output_dir,table_num,Y1_TABLES,QUEXIAN_XIANGQING_DIR,Y1_TABLES_PICTURES,4,5,total_table_num,False,xuhao=Xu_Hao)
|
||||
total_table_num,table_num,Xu_Hao = await add_dynamic_table(output_doc,output_dir,table_num,Y2_TABLES,QUEXIAN_XIANGQING_DIR,Y2_TABLES_PICTURES,4,5,total_table_num,False,xuhao=Xu_Hao)
|
||||
total_table_num,table_num,Xu_Hao = await add_dynamic_table(output_doc,output_dir,table_num,Y3_TABLES,QUEXIAN_XIANGQING_DIR,Y3_TABLES_PICTURES,4,5,total_table_num,False,xuhao=Xu_Hao)
|
||||
|
||||
if baogao_info['if_docx_conclusion']:
|
||||
#总结
|
||||
ZONG_JIE_DIR = os.path.join(muban_dir,"result.docx")
|
||||
ZONG_JIE_BEFORE = "result"
|
||||
ZONG_JIE = baogao_info['baogao_zongjie']
|
||||
print(add_documents(output_dir, ZONG_JIE_DIR))
|
||||
print(await search_and_replace(output_dir, ZONG_JIE_BEFORE, ZONG_JIE))
|
||||
print(await search_and_replace(output_dir, 'company_yi', Yi_company))
|
||||
print(await search_and_replace(output_dir, 'baogao_date', baogao_date.split(' ')[0]))
|
||||
print(await search_and_replace(output_dir, jiegou_xuhao, num_to_chinese[head_num]))
|
||||
|
||||
from tools.argtool import (load_config, parse_arguments, get_default_config,
|
||||
merge_configs)
|
||||
|
||||
def main():
|
||||
args = parse_arguments()
|
||||
|
||||
json_data1 = {
|
||||
"turbine_id": args.turbine_id,
|
||||
}
|
||||
|
||||
json_data2 = {
|
||||
'shengcheng_dir': args.output_dir,
|
||||
'muban_dir': args.template_dir,
|
||||
"dianxing_enum" : args.dianxing_enum, # 典型图的枚举值
|
||||
"quexian_enum" : args.quexian_enum, # 缺陷图的枚举值
|
||||
"other_enum" : args.other_enum, # 其他图的枚举值
|
||||
"waibu_enum" : args.waibu_enum,
|
||||
"neibu_enum" : args.neibu_enum,
|
||||
"fanglei_enum" : args.fanglei_enum,
|
||||
"if_waibu" : args.if_waibu,
|
||||
"if_neibu" : args.if_neibu,
|
||||
"if_fanglei" : args.if_fanglei,
|
||||
"userName" : args.username,
|
||||
"baogaoCheck" : args.baogaoCheck,
|
||||
'key_words': args.keywords, #关键字,用于汇总图的名字包含缺陷时标红,匹配逻辑为正则匹配单个字则为红 后续可优化
|
||||
"shigong_fangan" : None,
|
||||
'jiancha_renyuan': args.jiancha_renyuan,
|
||||
"check_date" : args.check_date,
|
||||
"data_processor" : args.data_processor,
|
||||
"coverurl" : args.coverurl,
|
||||
"baogao_zongjie" : args.conclusion,
|
||||
}
|
||||
|
||||
json_data2['shengcheng_dir'] = r"/home/dtyx/桌面/yhh/Report_Generate_Server/output"
|
||||
json_data2['muban_dir'] = r"/home/dtyx/桌面/yhh/Report_Generate_Server/muban"
|
||||
|
||||
asyncio.run(generate_report(json_data1, json_data2))
|
||||
default_cfg = get_default_config()
|
||||
|
||||
# 读取配置文件(若存在)
|
||||
file_cfg = {}
|
||||
if Path(args.config_file).exists():
|
||||
try:
|
||||
file_cfg = load_config(args.config_file)
|
||||
except Exception as e:
|
||||
print(f'警告:加载配置文件失败 {e},使用默认配置')
|
||||
|
||||
merged = merge_configs(default_cfg, file_cfg, args)
|
||||
|
||||
# 补全必要目录
|
||||
if not merged['json2']['shengcheng_dir']:
|
||||
merged['json2']['shengcheng_dir'] = str(Path.cwd() / 'output')
|
||||
if not merged['json2']['muban_dir']:
|
||||
merged['json2']['muban_dir'] = str(Path.cwd() / 'muban')
|
||||
|
||||
Path(merged['json2']['shengcheng_dir']).mkdir(parents=True, exist_ok=True)
|
||||
|
||||
asyncio.run(generate_report(merged['json1'], merged['json2']))
|
||||
print('文档生成完毕')
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
|
|
@ -1 +1,3 @@
|
|||
报告生成接入服务器的测试开发(调用服务器的api测试)
|
||||
#### 报告生成接入服务器的测试开发(调用服务器的api测试)
|
||||
|
||||
### 在dist目录查看编译好的文件,和使用文档
|
|
@ -0,0 +1,82 @@
|
|||
# 报告生成工具使用文档
|
||||
|
||||
## 1. 功能简介
|
||||
本工具用于根据风机巡检数据自动生成 Word 报告。
|
||||
支持 **配置文件** 与 **命令行参数** 两种输入方式,命令行可覆盖配置文件中的同名字段。
|
||||
|
||||
## 2. 安装与准备
|
||||
1. 克隆或下载代码。
|
||||
2. 安装依赖(仅需标准库,无需额外包)。
|
||||
3. 准备模板目录(默认 `./muban`)与输出目录(默认 `./output`)。
|
||||
|
||||
## 3. 配置文件(推荐)
|
||||
在项目根目录新建 `config.json`(或任意文件名),格式如下:
|
||||
|
||||
```json
|
||||
{
|
||||
"json1": {
|
||||
"turbine_id": "183463dbf40d9278549a76b82b175dd9"
|
||||
},
|
||||
"json2": {
|
||||
"shengcheng_dir": "./reports",
|
||||
"muban_dir": "./templates",
|
||||
"if_waibu": true,
|
||||
"if_neibu": true,
|
||||
"if_fanglei": true,
|
||||
"userName": "张三",
|
||||
"baogaoCheck": "已审核",
|
||||
"key_words": "缺,损,裂",
|
||||
"data_processor": "李四",
|
||||
"jiancha_renyuan": "王五",
|
||||
"check_date": "2024-06-01",
|
||||
"coverurl": "https://example.com/cover.jpg",
|
||||
"conclusion": "整体良好,详见报告"
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
4. 命令行参数
|
||||
|
||||
| 参数 | 说明 | 示例 |
|
||||
|------|------|------|
|
||||
| `--config` / `-c` | 指定配置文件 | `--config prod.json` |
|
||||
| `--turbine_id` / `--id` | 风机 ID | `--turbine_id 123456` |
|
||||
| `--output_dir` / `--out` | 报告输出目录 | `--output_dir ./result` |
|
||||
| `--template_dir` / `--tpl` | 模板目录 | `--template_dir ./tpl` |
|
||||
| `--if_waibu` | 是否包含外部作业章节 | `true` / `false` |
|
||||
| `--if_neibu` | 是否包含内部作业章节 | `true` / `false` |
|
||||
| `--if_fanglei` | 是否包含防雷作业章节 | `true` / `false` |
|
||||
| `--userName` | 报告编制人 | `--userName 张三` |
|
||||
| `--baogaoCheck` | 审核状态 | `--baogaoCheck 已审核` |
|
||||
| `--key_words` | 关键词(英文逗号分隔) | `--key_words 缺,损,裂` |
|
||||
| `--data_processor` | 数据处理人 | `--data_processor 李四` |
|
||||
| `--jiancha_renyuan` | 检查人员 | `--jiancha_renyuan 王五` |
|
||||
| `--check_date` | 检查日期(YYYY-MM-DD) | `--check_date 2024-06-01` |
|
||||
| `--coverurl` | 封面图片 URL | `--coverurl /stastic/path/to/pic` |
|
||||
| `--conclusion` | 报告总结 | `--conclusion 整体良好` |
|
||||
|
||||
5. 使用示例
|
||||
|
||||
### 5.1 仅使用配置文件
|
||||
|
||||
```bash
|
||||
python report.py
|
||||
```
|
||||
|
||||
### 5.2 完全命令行
|
||||
|
||||
```bash
|
||||
python report.py \
|
||||
--turbine_id 123456 \
|
||||
--output_dir ./my_out \
|
||||
--if_waibu false \
|
||||
--userName Alice \
|
||||
--check_date 2024-06-01 \
|
||||
--conclusion "检查完毕,无异常"
|
||||
```
|
||||
|
||||
### 5.3 混合方式
|
||||
|
||||
```bash
|
||||
python report.py --config prod.json --userName Bob --if_fanglei false
|
||||
```
|
|
@ -0,0 +1,111 @@
|
|||
from typing import Dict, Any
|
||||
import json
|
||||
import argparse
|
||||
def load_config(config_path: str) -> Dict[str, Any]:
|
||||
"""从JSON配置文件加载配置"""
|
||||
with open(config_path, 'r', encoding='utf-8') as f:
|
||||
return json.load(f)
|
||||
|
||||
def parse_arguments():
|
||||
parser = argparse.ArgumentParser(description='报告生成工具')
|
||||
|
||||
# 添加配置文件参数
|
||||
parser.add_argument('--config', '-c', dest='config_file',
|
||||
default='config.json',
|
||||
help='配置文件路径,默认为当前目录下的config.json')
|
||||
|
||||
# 保留一些关键参数作为命令行可覆盖选项
|
||||
parser.add_argument('--turbine_id', '--id', dest='turbine_id',
|
||||
help='覆盖配置文件中的风机ID')
|
||||
parser.add_argument('--output_dir', '--out', dest='output_dir',
|
||||
help='覆盖配置文件中的输出目录')
|
||||
parser.add_argument('--template_dir', '--tpl', dest='template_dir',
|
||||
help='覆盖配置文件中的模板目录')
|
||||
parser.add_argument('--if_waibu', type=lambda x: x.lower() in ('true', '1', 'yes'),
|
||||
help='是否包含外部作业章节')
|
||||
parser.add_argument('--if_neibu', type=lambda x: x.lower() in ('true', '1', 'yes'),
|
||||
help='是否包含内部作业章节')
|
||||
parser.add_argument('--if_fanglei', type=lambda x: x.lower() in ('true', '1', 'yes'),
|
||||
help='是否包含防雷作业章节')
|
||||
parser.add_argument('--userName', dest='userName',
|
||||
help='报告编制人')
|
||||
parser.add_argument('--baogaoCheck', dest='baogaoCheck',
|
||||
help='报告审核状态')
|
||||
parser.add_argument('--key_words', dest='key_words',
|
||||
help='关键词,英文逗号分隔')
|
||||
parser.add_argument('--data_processor', dest='data_processor',
|
||||
help='数据处理人')
|
||||
parser.add_argument('--jiancha_renyuan', dest='jiancha_renyuan',
|
||||
help='检查人员')
|
||||
parser.add_argument('--check_date', dest='check_date',
|
||||
help='检查日期,格式 YYYY-MM-DD')
|
||||
parser.add_argument('--coverurl', dest='coverurl',
|
||||
help='封面图片 URL')
|
||||
parser.add_argument('--conclusion', dest='conclusion',
|
||||
help='报告总结')
|
||||
return parser.parse_args()
|
||||
|
||||
def merge_configs(default_cfg: Dict[str, Any],
|
||||
file_cfg: Dict[str, Any],
|
||||
cli: argparse.Namespace) -> Dict[str, Dict[str, Any]]:
|
||||
"""合并默认配置、文件配置和命令行参数"""
|
||||
|
||||
merged = {
|
||||
'json1': {**default_cfg['json1'], **file_cfg.get('json1', {})},
|
||||
'json2': {**default_cfg['json2'], **file_cfg.get('json2', {})}
|
||||
}
|
||||
|
||||
# 高频覆盖项
|
||||
if cli.turbine_id:
|
||||
merged['json1']['turbine_id'] = cli.turbine_id
|
||||
if cli.output_dir:
|
||||
merged['json2']['shengcheng_dir'] = cli.output_dir
|
||||
if cli.template_dir:
|
||||
merged['json2']['muban_dir'] = cli.template_dir
|
||||
for key in ('if_waibu', 'if_neibu', 'if_fanglei', 'userName',
|
||||
'baogaoCheck', 'key_words', 'data_processor',
|
||||
'jiancha_renyuan', 'check_date', 'coverurl', 'conclusion'):
|
||||
val = getattr(cli, key, None)
|
||||
if val is not None:
|
||||
merged['json2'][key] = val
|
||||
|
||||
return merged
|
||||
def get_default_config() -> Dict[str, Dict[str, Any]]:
|
||||
"""获取默认配置"""
|
||||
return {
|
||||
'json1': {
|
||||
"turbine_id": "183463dbf40d9278549a76b82b175dd9",
|
||||
},
|
||||
'json2': {
|
||||
'shengcheng_dir': "",
|
||||
'muban_dir': "",
|
||||
"dianxing_enum": "TYPICAL",
|
||||
"quexian_enum": "DEFECT",
|
||||
"other_enum": "OTHER",
|
||||
"waibu_enum": "out-work",
|
||||
"neibu_enum": "in-work",
|
||||
"fanglei_enum": "lightning-protection-work",
|
||||
"if_waibu": True,
|
||||
"if_neibu": True,
|
||||
"if_fanglei": True,
|
||||
"userName": "admin",
|
||||
"baogaoCheck": "未审核",
|
||||
'key_words': '缺,损,裂,脱,污',
|
||||
"data_processor": "未获取",
|
||||
"shigong_fangan": None,
|
||||
'jiancha_renyuan': '未获取',
|
||||
"check_date": None,
|
||||
"coverurl": None,
|
||||
"conclusion": "未填写总结",
|
||||
"if_docx_fengmian": True,
|
||||
"if_docx_project_overview": True,
|
||||
"if_docx_inspection_method": True,
|
||||
"if_docx_inspection_info": True,
|
||||
"if_docx_chengguo_sub": True,
|
||||
"if_docx_inspection_text": True,
|
||||
"if_docx_inspection_picture": True,
|
||||
"if_docx_defect_picture": True,
|
||||
"if_docx_conclusion": True,
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue