fix: 适配没有英语名字的情况
This commit is contained in:
@@ -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文件生成属相)
|
||||
# ==========================================
|
||||
|
||||
Reference in New Issue
Block a user