Report_Generate_Server/tools/esay_docx_func.py

183 lines
7.3 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from content_tools import add_picture_to_table, search_and_replace,add_picture
from get_pictures import resize_and_reduce_quality
from document_tools import add_table_to_document,add_documents
from docx import Document
from typing import List
def fill_tables(Y_table_list, row, col, Y_Table_num, Y):
"""根据前端返回json块填写表格list并实时跟进已填写表格数量
目前只支持固定的缺陷图的填写
Args:
Y_table_list (list): 前端返回的json块
row (int): 表格行数
col (int): 表格列数
Y_Table_num: json块中有几个表格
Xu_Hao: 是第几个json块
Y: 其他参数
Return:
Y1_TABLES: 三维,表和对应元素
table_index_to_images: 字典,表索引到图片路径列表的映射
Xu_Hao到达第几个表了
"""
table_index_to_images = {}
Y_TABLES = [[["" for _ in range(row)] for _ in range(col)] for _ in range(Y_Table_num)]
# 处理前端返回数据
for l, table_dict in enumerate(Y_table_list):
if table_dict:
Y_TABLES[l][1][0] = Y
Y_TABLES[l][1][1] = table_dict["QueXianLeiXing"]
Y_TABLES[l][1][2] = table_dict["QueXianWeiZhi"]
Y_TABLES[l][1][3] = table_dict["QueXianChiCun"]
Y_TABLES[l][3][0] = table_dict["WeiZongDengJi"]
Y_TABLES[l][3][1] = table_dict["visibility"]
Y_TABLES[l][3][2] = table_dict["urgency"]
Y_TABLES[l][3][3] = table_dict["repair_suggestion"]
# 获取图片路径
image_path = table_dict['Tupian_Dir']
if image_path:
# 确保路径是字符串形式
if isinstance(image_path, list):
table_index_to_images[l] = image_path.copy()
else:
table_index_to_images[l] = [str(image_path)]
return Y_TABLES, table_index_to_images
async def process_images_table(data_dict, output_dir, start_i, JIANCHA_NEIRONG_PICTURES_TABLE, key_words = None):
"""添加对应表格且填写图片名与插入图片
Args:
data_dict (dict): dict内容图片:图片路径
output_dir (str): 输出路径
start_i (int): 总表格数量
JIANCHA_NEIRONG_PICTURES_TABLE (str): 二维表模板路径
Returns:
int: 最后使用的表格序号
"""
items = list(data_dict.items())
picture_num = len(items)
line_index = 0
picture_index = 0
i = start_i
for content_row in range(((picture_num + 2) // 3) * 2):
if content_row % 2 == 1:
# 文字行(从 items 取图片名)
JIANCHA_NEIRONG_TEXT = [["" for _ in range(3)] for _ in range(1)] # 1行3列
for k in range(1): # 只有1行
for l in range(3):
if line_index >= picture_num:
break
JIANCHA_NEIRONG_TEXT[k][l] = items[line_index][0] # 图片名
print(f'当前为文字表格,在({k},{l})位置插入文字: {items[line_index][0]}')
line_index += 1
print(f"当前待插入表格: {JIANCHA_NEIRONG_TEXT}")
print(f"当前表格序号为 {i}")
output_doc, message = await add_table_to_document(
output_dir, JIANCHA_NEIRONG_PICTURES_TABLE, 1, 3, i, JIANCHA_NEIRONG_TEXT, False, None, key_words
)
i += 1
else:
# 图片行(从 items 取图片路径)
print(f"当前表格序号为 {i}")
output_doc, message = await add_table_to_document(
output_dir, JIANCHA_NEIRONG_PICTURES_TABLE, 1, 3, i, None, False
)
for k in range(3):
if picture_index < picture_num:
pic_path = items[picture_index][1] # 图片路径
print(f"当前为图片表格,在(0,{k})位置插入图片: {pic_path}")
print(add_picture_to_table(output_doc, output_dir, 0, k, pic_path, i, 1.8898))
picture_index += 1
i += 1
print(message)
return i # 返回最后使用的表格序号
async def add_dynamic_table(output_doc, output_dir, table_num, TABLES, JIANCHA_XIANGQING_DIR, PICTURES, row, col, i, FLAG, xuhao):
"""创建动态表
Args:
output_doc (Document): 文档对象
output_dir (str): 输出目录
table_num (int): 表格序号
TABLES (list): 表格数据
JIANCHA_XIANGQING_DIR (str): 检查详情表目录
PICTURES (dict): 图片数据字典,键为表索引,值为图片路径列表
row (int): 行数
col (int): 列数
i (int): 表格序号
FLAG: 其他标志
Returns:
tuple: (i, table_num) 更新后的表格序号和表格数量
"""
for table_idx, Table in enumerate(TABLES):
print(Table)
output_doc, message = await add_table_to_document(output_dir, JIANCHA_XIANGQING_DIR, row, col, i, Table, FLAG)
print(message)
# 获取当前表格对应的图片
current_table_pictures = PICTURES.get(table_idx, [])
print(f"开始处理图片列表: {current_table_pictures}")
for picturedir in current_table_pictures:
try:
print(f"添加 {picturedir} {type(picturedir)}到表格{table_idx}")
resize_and_reduce_quality(picturedir, picturedir)
add_picture_to_table(output_doc, output_dir, 4, 0, picturedir, i, 4.7232)
except Exception as e:
print(f"添加图片失败:{e}")
print(await search_and_replace(output_dir, 'tupian_xuhao', f'{xuhao}'))
table_num += 1
i += 1
xuhao += 1
return i, table_num, xuhao
def get_year_month(date):
"""根据格式化date字符串获取年月 'date': '二〇二一年十二月十日 9:00'
Args: date (str): 日期字符串
Returns: 年月字符串 '二〇二一年十二月'
"""
unit_map = {'1' : '', '2' : '', '3' : '', '4' : '', '5' : '', '6' : '', '7' : '', '8' : '', '9' : '', '0' : ''}
unit_map_month = {1 : '', 2 : '', 3 : '', 4 : '', 5 : '', 6 : '', 7 : '', 8 : '', 9 : '', 10 : '', 11 : '十一', 12 : '十二'}
year = date.split('')[0]
month = date.split('')[1].split('')[0]
year = ''.join([unit_map[i] for i in year])
month = unit_map_month[int(month)]
return f"{year}{month}"
def merge_info(frontend_info, default_info):
"""
合并前端传入的 info 和默认 info
规则如果前端传入的值为空None 或空字符串),则使用默认值
Args:
frontend_info: 前端传入的字典
default_info: 默认的完整字典
Returns:
合并后的完整字典
"""
if not isinstance(frontend_info, dict) or frontend_info is None:
return default_info.copy()
merged_info = {}
for key, default_value in default_info.items():
# 获取前端传入的值
frontend_value = frontend_info.get(key)
# 判断前端值是否为空None 或空字符串)
if frontend_value is None or frontend_value == "":
merged_info[key] = default_value
else:
merged_info[key] = frontend_value
return merged_info