fix:优化启动方式
This commit is contained in:
@@ -3,7 +3,10 @@
|
||||
# ==========================================
|
||||
import os
|
||||
|
||||
from loguru import logger
|
||||
|
||||
from utils.font_utils import is_font_available
|
||||
from utils.image_utils import get_corrected_image_stream
|
||||
|
||||
|
||||
def replace_text_in_slide(prs, slide_index, placeholder, text):
|
||||
@@ -94,18 +97,36 @@ def replace_text_in_slide(prs, slide_index, placeholder, text):
|
||||
|
||||
|
||||
def replace_picture(prs, slide_index, placeholder, img_path):
|
||||
"""在指定幻灯片中替换指定占位符的图片"""
|
||||
"""在指定幻灯片中替换指定占位符的图片(包含自动旋转修复)"""
|
||||
if not os.path.exists(img_path):
|
||||
print(f"警告: 图片路径不存在 {img_path}")
|
||||
logger.warning(f"警告: 图片路径不存在 {img_path}")
|
||||
return
|
||||
|
||||
slide = prs.slides[slide_index]
|
||||
sp_tree = slide.shapes._spTree
|
||||
|
||||
target_shape = None
|
||||
target_index = -1
|
||||
|
||||
# 1. 先找到目标形状和它的索引
|
||||
for i, shape in enumerate(slide.shapes):
|
||||
if shape.name == placeholder:
|
||||
left, top, width, height = shape.left, shape.top, shape.width, shape.height
|
||||
sp_tree.remove(shape._element)
|
||||
new_shape = slide.shapes.add_picture(img_path, left, top, width, height)
|
||||
sp_tree.insert(i, new_shape._element)
|
||||
target_shape = shape
|
||||
target_index = i
|
||||
break
|
||||
|
||||
if target_shape:
|
||||
# 获取原位置信息
|
||||
left, top, width, height = target_shape.left, target_shape.top, target_shape.width, target_shape.height
|
||||
|
||||
# 2. 获取修正后的图片流
|
||||
img_stream = get_corrected_image_stream(img_path)
|
||||
|
||||
# 3. 移除旧形状
|
||||
sp_tree.remove(target_shape._element)
|
||||
|
||||
# 4. 插入新图片 (使用流而不是路径)
|
||||
new_shape = slide.shapes.add_picture(img_stream, left, top, width, height)
|
||||
|
||||
# 5. 恢复层级位置 (z-order)
|
||||
sp_tree.insert(target_index, new_shape._element)
|
||||
|
||||
Reference in New Issue
Block a user