智能设备开发中TextBox 的 Alignment 属性始终对齐在其左侧

本文介绍了一个关于TextBox在Windows CE设备上的对齐问题,并提供了解决方案:通过将文本框的MultiLine属性设置为True来实现正确的文本对齐。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我从微软上看的 可惜我还没解决 希望对大家有所帮助  这方面的资料是在太少了

 

将 TextBox 中的对齐属性进行设置它即具有不影响上放置一个文本框的文本的对齐。 文本始终显示在左侧两端对齐。 这是 Windows CE 设备上和仿真程序中。

 

 

 解决方法:

 

将文本框的 MultiLine 属性设置为 True。

import re import os from pptx import Presentation from pptx.util import Inches, Pt from pptx.enum.text import PP_ALIGN from PIL import Image import math def natural_sort_key(s): """ 自然排序键函数,确保数字按数值大小排序 例如: ['10', '2'] 会排序为 ['2', '10'] """ return [int(text) if text.isdigit() else text.lower() for text in re.split(r'(\d+)', s)] def create_ppt_from_folders(folder_path, output_ppt_path): """ 将图片文件夹整合到PPT,每个文件夹一张幻灯片,图片占据上部3/5区域 确保按文件夹序号顺序排列 """ # 创建PPT对象 prs = Presentation() # 设置幻灯片尺寸(16:9) prs.slide_width = Inches(13.33) # 宽度 prs.slide_height = Inches(7.5) # 高度 # 获取所有文件夹(按自然顺序排序) folders = [f for f in os.listdir(folder_path) if os.path.isdir(os.path.join(folder_path, f))] folders.sort(key=natural_sort_key) # 使用自然排序 if not folders: print("未找到任何图片文件夹") return print(f"共发现 {len(folders)} 个文件夹,将按序号顺序创建幻灯片") # 遍历每个文件夹(已按正确顺序) for folder in folders: folder_full_path = os.path.join(folder_path, folder) # 创建新幻灯片 slide = prs.slides.add_slide(prs.slide_layouts[6]) # 添加标题(文件夹名称) title = slide.shapes.add_textbox(Inches(0.5), Inches(0.2), Inches(12), Inches(0.5)) title_frame = title.text_frame title_para = title_frame.add_paragraph() title_para.text = f"图片集: {folder}" title_para.font.bold = True title_para.font.size = Pt(24) title_para.alignment = PP_ALIGN.CENTER # 获取文件夹中的所有图片(按文件名自然排序) image_files = [f for f in os.listdir(folder_full_path) if f.lower().endswith(('.png', '.jpg', '.jpeg', '.gif', '.bmp'))] image_files.sort(key=natural_sort_key) # 图片也使用自然排序 if not image_files: print(f"文件夹 '{folder}' 中没有找到图片") continue # 计算图片区域尺寸(占据幻灯片上部3/5) slide_height = prs.slide_height top_area_height = slide_height * 3 / 5 # 上部3/5区域高度 bottom_area_height = slide_height * 2 / 5 # 下部2/5区域高度 # 根据图片数量确定布局 img_count = len(image_files) max_per_row = 3 # 每行最多图片数 # 计算行数和列数 rows = min(3, (img_count + max_per_row - 1) // max_per_row) # 最多3行 cols = min(max_per_row, img_count) if img_count < max_per_row else max_per_row # 计算单个图片尺寸 img_width = Inches(3) if cols <= 1 else Inches(10) / cols img_height = top_area_height / rows - Inches(0.2) # 留出间距 # 添加所有图片到幻灯片 print(f"在幻灯片 '{folder}' 中添加 {img_count} 张图片...") for i, img_file in enumerate(image_files): img_path = os.path.join(folder_full_path, img_file) # 获取图片原始尺寸(用于保持宽高比) with Image.open(img_path) as img: orig_width, orig_height = img.size aspect_ratio = orig_width / orig_height # 计算图片位置 row = i // cols col = i % cols # 计算位置坐标 left = Inches(1) + col * (img_width + Inches(0.2)) top = Inches(1) + row * img_height # 调整尺寸保持宽高比 if img_height * aspect_ratio <= img_width: # 高度受限 width = img_height * aspect_ratio height = img_height left += (img_width - width) / 2 # 水平居中 else: # 宽度受限 width = img_width height = img_width / aspect_ratio top += (img_height - height) / 2 # 垂直居中 # 添加图片到幻灯片 try: slide.shapes.add_picture(img_path, left, top, width, height) except Exception as e: print(f"添加图片失败: {img_path} - {str(e)}") # 在下部区域添加说明文字 #textbox = slide.shapes.add_textbox(Inches(1), top_area_height + Inches(0.5), # Inches(10), bottom_area_height - Inches(1)) # tf = textbox.text_frame # tf.word_wrap = True # p = tf.add_paragraph() # p.text = f"文件夹 '{folder}' 包含 {img_count} 张图片" # p.font.size = Pt(18) # p.alignment = PP_ALIGN.CENTER # 保存PPT prs.save(output_ppt_path) print(f"\nPPT创建成功!共 {len(folders)} 张幻灯片,按文件夹序号顺序排列") if __name__ == "__main__": # 配置路径 image_folders_path = "E:/pythonproject313/转换/pic" # 图片文件夹根目录 output_ppt = "E:/pythonproject313/转换/图片展示.pptx" # 输出PPT文件路径 create_ppt_from_folders(image_folders_path, output_ppt) 将该代码中只有一张图片的幻灯片中图片左对齐位置与两张图片的幻灯片保持一致
最新发布
08-13
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值