fix:实现配置功能,实现园长一键签名功能
This commit is contained in:
@@ -3,7 +3,7 @@ import os
|
||||
import time
|
||||
from loguru import logger
|
||||
import zipfile
|
||||
|
||||
import traceback
|
||||
|
||||
def export_templates_folder(output_folder, stop_event, progress_callback=None):
|
||||
"""
|
||||
@@ -217,3 +217,27 @@ def check_file_exists(file_path):
|
||||
判断文件是否存在
|
||||
"""
|
||||
return file_path and isinstance(file_path, str) and os.path.exists(file_path)
|
||||
|
||||
def get_output_pptx_files(output_dir="output"):
|
||||
"""
|
||||
获取 output 文件夹下所有的 pptx 文件
|
||||
:param output_dir: output 文件夹路径
|
||||
"""
|
||||
try:
|
||||
folder_path = os.path.abspath(output_dir)
|
||||
if not os.path.exists(folder_path):
|
||||
logger.error(f"文件夹不存在: {folder_path}")
|
||||
return
|
||||
# 获取所有 ppt/pptx 文件
|
||||
files = [
|
||||
f for f in os.listdir(folder_path) if f.lower().endswith((".ppt", ".pptx"))
|
||||
]
|
||||
if not files:
|
||||
logger.warning("没有找到 PPT 文件")
|
||||
return
|
||||
total_count = len(files)
|
||||
logger.info(f"发现 {total_count} 个文件,准备开始转换...")
|
||||
return files
|
||||
except Exception as e:
|
||||
logger.error(f"发生未知错误: {e}")
|
||||
logger.error(traceback.format_exc())
|
||||
|
||||
@@ -12,7 +12,7 @@ import traceback
|
||||
import comtypes.client
|
||||
from config.config import load_config
|
||||
from utils.agent_utils import generate_comment
|
||||
from utils.file_utils import check_file_exists
|
||||
from utils.file_utils import check_file_exists, get_output_pptx_files
|
||||
from utils.image_utils import find_image_path
|
||||
from utils.zodiac_utils import calculate_zodiac
|
||||
from utils.growt_utils import (
|
||||
@@ -22,6 +22,7 @@ from utils.growt_utils import (
|
||||
replace_four_page,
|
||||
replace_five_page,
|
||||
)
|
||||
from utils.pptx_utils import replace_picture
|
||||
|
||||
# 如果你之前没有全局定义 console,这里定义一个
|
||||
console = Console()
|
||||
@@ -331,7 +332,7 @@ def generate_report(stop_event: threading.Event = None, progress_callback=None):
|
||||
|
||||
|
||||
# ==========================================
|
||||
# 5. 转换格式(根据names.xlsx文件生成PPT转PDF)
|
||||
# 4. 转换格式(根据names.xlsx文件生成PPT转PDF)
|
||||
# ==========================================
|
||||
def batch_convert_folder(folder_path, stop_event: threading.Event = None, progress_callback=None):
|
||||
"""
|
||||
@@ -496,3 +497,49 @@ def generate_zodiac(stop_event: threading.Event = None, progress_callback=None):
|
||||
except Exception as e:
|
||||
logger.error(f"程序运行出错: {str(e)}")
|
||||
logger.error(traceback.format_exc())
|
||||
|
||||
# ==========================================
|
||||
# 6. 一键生成园长签名(根据输出文件夹生成签名)
|
||||
# ==========================================
|
||||
def generate_signature(progress_callback=None) -> str:
|
||||
"""
|
||||
生成园长签名
|
||||
"""
|
||||
try:
|
||||
# 获取所有的PPT (此时返回的是文件名或路径的列表)
|
||||
pptx_files = get_output_pptx_files(config["output_folder"])
|
||||
|
||||
if not pptx_files:
|
||||
logger.warning("没有找到 PPT 文件")
|
||||
return "未找到文件"
|
||||
|
||||
logger.info(f"开始生成签名,共 {len(pptx_files)} 个 PPT 文件...")
|
||||
|
||||
img_path = config.get("signature_image") # 签名图片路径
|
||||
if not img_path or not os.path.exists(img_path):
|
||||
logger.error(f"签名图片不存在: {img_path}")
|
||||
logger.warning(f"⚠️ 警告: 缺少签名照片('signature')")
|
||||
return
|
||||
logger.info(f"签名图片存在: {img_path}")
|
||||
for i, filename in enumerate(pptx_files):
|
||||
# 获取完整绝对路径
|
||||
pptx_path = os.path.join(config["output_folder"], filename)
|
||||
|
||||
# --- 关键修改点 1: 打开 PPT 对象 ---
|
||||
prs = Presentation(pptx_path)
|
||||
|
||||
# --- 关键修改点 2: 传递 prs 对象而不是路径字符串 ---
|
||||
replace_picture(prs, 1, "signature", img_path)
|
||||
|
||||
# --- 关键修改点 3: 保存修改后的 PPT ---
|
||||
prs.save(pptx_path)
|
||||
|
||||
# 更新进度条 (如果有 callback)
|
||||
if progress_callback:
|
||||
progress_callback(i + 1, len(pptx_files),f"[{i + 1}/{len(pptx_files)}] 生成签名完成: {filename}")
|
||||
logger.success(f"[{i + 1}/{len(pptx_files)}] 生成签名完成: {filename}")
|
||||
if progress_callback:
|
||||
progress_callback(len(pptx_files), len(pptx_files), "签名生成完成")
|
||||
except Exception as e:
|
||||
logger.error(f"generate_signature 发生未知错误: {e}")
|
||||
return str(e)
|
||||
@@ -97,7 +97,15 @@ def replace_text_in_slide(prs, slide_index, placeholder, text):
|
||||
|
||||
|
||||
def replace_picture(prs, slide_index, placeholder, img_path):
|
||||
"""在指定幻灯片中替换指定占位符的图片(包含自动旋转修复)"""
|
||||
"""
|
||||
在指定幻灯片中替换指定占位符的图片(包含自动旋转修复)
|
||||
|
||||
参数:
|
||||
prs: Presentation 对象
|
||||
slide_index: 幻灯片索引 (从0开始)
|
||||
placeholder: 占位符名称 (例如 "signature")
|
||||
img_path: 图片路径
|
||||
"""
|
||||
if not os.path.exists(img_path):
|
||||
logger.warning(f"警告: 图片路径不存在 {img_path}")
|
||||
return
|
||||
@@ -129,4 +137,4 @@ def replace_picture(prs, slide_index, placeholder, img_path):
|
||||
new_shape = slide.shapes.add_picture(img_stream, left, top, width, height)
|
||||
|
||||
# 5. 恢复层级位置 (z-order)
|
||||
sp_tree.insert(target_index, new_shape._element)
|
||||
sp_tree.insert(target_index, new_shape._element)
|
||||
24
utils/template_utils.py
Normal file
24
utils/template_utils.py
Normal file
@@ -0,0 +1,24 @@
|
||||
import os
|
||||
from config.config import get_base_dir
|
||||
|
||||
def get_template_files():
|
||||
"""
|
||||
遍历 templates 目录,返回所有 PPTX 文件的文件名列表
|
||||
"""
|
||||
# 获取 templates 文件夹的绝对路径
|
||||
# 这里的 get_base_dir() 是你之前定义的函数
|
||||
base_dir = get_base_dir()
|
||||
templates_dir = os.path.join(base_dir, 'templates')
|
||||
|
||||
# 检查目录是否存在,不存在则返回空列表
|
||||
if not os.path.exists(templates_dir):
|
||||
return []
|
||||
|
||||
# 遍历目录
|
||||
files = []
|
||||
for filename in os.listdir(templates_dir):
|
||||
# 过滤掉隐藏文件,并只保留 .pptx 结尾的文件
|
||||
if not filename.startswith('.') and filename.endswith('.pptx'):
|
||||
files.append(filename)
|
||||
|
||||
return sorted(files)
|
||||
Reference in New Issue
Block a user