fix: 适配没有英语名字的情况

This commit is contained in:
2025-12-12 17:24:32 +08:00
parent cbf87d2569
commit 9d347f9bc9
9 changed files with 181 additions and 34 deletions

View File

@@ -191,3 +191,14 @@ def initialize_project(root_dir="."):
logger.warning(
f"⚠️ 警告: 模板文件不存在 ({src_excel})data 文件夹内将没有 Excel 文件。"
)
def check_file_exists(file_path):
"""
判断文件是否存在
"""
if (file_path and isinstance(file_path, str) and os.path.exists(file_path)):
logger.info(f"✅ 文件存在: {file_path}")
return True
else:
logger.error(f"❌ 文件不存在: {file_path}")
return False

View File

@@ -11,6 +11,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.font_utils import install_fonts_from_directory
from utils.image_utils import find_image_path
from utils.zodiac_utils import calculate_zodiac
@@ -222,47 +223,50 @@ def generate_report():
# --- 页面 3 ---
student_image_folder = os.path.join(config["image_folder"], name)
logger.info(f"学生:{name},图片文件夹: {student_image_folder}")
if os.path.exists(student_image_folder):
me_image_path = find_image_path(student_image_folder, "me")
# 判断学生图片文件夹是否存在
if not os.path.exists(student_image_folder):
logger.warning(
f"⚠️ 警告: 学生:{name},学生图片文件夹不存在 {student_image_folder}"
)
continue
# 构造信息字典供 helper 使用
info_dict = {
"name": name,
"english_name": english_name,
"sex": sex,
"birthday": birthday.strftime("%Y-%m-%d") if pd.notna(birthday) else "",
"zodiac": zodiac,
"friend": friend,
"hobby": hobby,
"game": game,
"food": food,
}
# 逻辑:必须同时满足 "不是None" 且 "是字符串" 且 "文件存在" 才能执行
if (
me_image_path
and isinstance(me_image_path, str)
and os.path.exists(me_image_path)
):
replace_three_page(prs, info_dict, me_image_path)
else:
# 只有在这里打印日志,告诉用户跳过了,但不中断程序
replace_three_page(prs, info_dict, None)
# 检查姓名是否为空
if not name:
logger.error(f"⚠️ 警告: 学生:{name},姓名为空,跳过")
break
# 构造学生信息字典
student_info_dict = {
"name": name,
"english_name": english_name if pd.notna(english_name) else "",
"sex": sex if pd.notna(sex) else "",
"birthday": (
birthday.strftime("%Y-%m-%d") if pd.notna(birthday) else ""
),
"zodiac": zodiac if pd.notna(zodiac) else "",
"friend": friend if pd.notna(friend) else "",
"hobby": hobby if pd.notna(hobby) else "",
"game": game if pd.notna(game) else "",
"food": food if pd.notna(food) else "",
}
# 获取学生个人照片路径
me_image_path = find_image_path(student_image_folder, "me")
# 检查学生图片是否存在,若不存在则跳过
if check_file_exists(me_image_path):
replace_three_page(prs, student_info_dict, me_image_path)
else:
logger.warning(f"⚠️ 警告: 学生{name},学生图片文件不存在 {student_image_folder}")
logger.warning(f"⚠️ 警告: 学生图片文件不存在 {me_image_path}")
# --- 页面 4 ---
class_image_path = find_image_path(
config["image_folder"], config["class_name"]
)
if (
class_image_path
and isinstance(class_image_path, str)
and os.path.exists(class_image_path)
):
# 添加检查班级图片是否存在,若不存在则跳过
if check_file_exists(class_image_path):
replace_four_page(prs, class_image_path)
else:
logger.warning(f"⚠️ 警告: 班级图片文件不存在 {class_image_path}")
# --- 页面 5 ---
if os.path.exists(student_image_folder):
img1_path = find_image_path(student_image_folder, "1")
@@ -335,10 +339,10 @@ def batch_convert_folder(folder_path):
try:
# 1. 启动应用 (只启动一次)
powerpoint = comtypes.client.CreateObject("PowerPoint.Application")
# 【建议】在后台线程运行时,有时设置为不可见更稳定,
# 但如果遇到转换卡死,可以尝试去掉下面这行的注释,让它显示出来
# powerpoint.Visible = 1
# powerpoint.Visible = 1
for filename in files:
ppt_path = os.path.join(folder_path, filename)
@@ -354,7 +358,7 @@ def batch_convert_folder(folder_path):
try:
# 打开 -> 另存为 -> 关闭
deck = powerpoint.Presentations.Open(ppt_path)
deck.SaveAs(pdf_path, 32) # 32 代表 PDF 格式
deck.SaveAs(pdf_path, 32) # 32 代表 PDF 格式
deck.Close()
except Exception as e:
logger.error(f"文件 {filename} 转换出错: {e}")
@@ -376,6 +380,7 @@ def batch_convert_folder(folder_path):
# 【核心修复 2】释放资源
pythoncom.CoUninitialize()
# ==========================================
# 5. 生成属相(根据names.xlsx文件生成属相)
# ==========================================