Report_Generate_Server/tools/dataproccess.py

99 lines
3.8 KiB
Python
Raw Normal View History

from tools.content_tools import add_picture_to_table
from tools.document_tools import add_table_to_document,search_and_replace
from tools.get_pictures import resize_and_reduce_quality
def caculate_work_days(start_date : str, end_date : str) -> str:
"""根据起止日期计算工期
Args:
start_date (str): 格式: yyyy-mm-ddThh:mm:ss
end_date (str): 格式: yyyy-mm-ddThh:mm:ss
Returns:
str: 计算出来的总工期单位为天
"""
import datetime
if start_date == None or end_date == None:
return f"开始时间{start_date} ---- 结束时间{end_date} 日期格式错误"
start_date = datetime.datetime.strptime(start_date, '%Y-%m-%dT%H:%M:%S')
end_date = datetime.datetime.strptime(end_date, '%Y-%m-%dT%H:%M:%S')
return (end_date - start_date).days
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)
await 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
新规则以default_info为基础字典用frontend_info完全覆写取两者的并集
Args:
frontend_info: 前端传入的字典
default_info: 默认的完整字典
Returns:
合并后的完整字典
"""
if not isinstance(frontend_info, dict) or frontend_info is None:
return default_info.copy()
# 先复制默认字典
merged_info = default_info.copy()
# 用前端字典完全覆写
merged_info.update(frontend_info)
return merged_info