Report_Generate_Server/tools/dataproccess.py

106 lines
4.0 KiB
Python
Raw 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 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
规则如果前端传入的值为空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