尝试使用 python-pptx 包在每张 PPTX 幻灯片的中心写入文本

问题描述 投票:0回答:1

我正在使用 python-pptx 创建一个 PPTX,其中包括在每张幻灯片的中心写入 1 个单词(从 python 脚本中提供的列表中提取); 例如,我的列表由 10 个单词组成,Python 脚本成功地创建了 10 张幻灯片的 1 个 PPTX,其中包含列表中的一个单词,每张幻灯片具有请求的字体大小; 所以,对于像我这样的初学者来说,这还不算太糟糕; 但是,我没有找到一种方法可以在每张幻灯片的中心写下这个单词;

这是Python脚本:

# import required things 
from pptx import Presentation  
from pptx.util import Inches, Pt 
from pptx.enum.text import MSO_ANCHOR
from pptx.enum.text import PP_ALIGN
import random
import json

adjectifs = ["grand", "petit", "beau", "laid", "joli", "moche", "intelligent", "stupide", "gentil", "méchant"]

# Mélange la liste des adjectifs
random.shuffle(adjectifs)
  
# Creating Object 
ppt = Presentation()  

# Get bounding box of shapes
# bounding_box = slide.shapes.bounding_box

# To create blank slide layout 
# We have to use 6 as an argument 
# of slide_layouts   
blank_slide_layout = ppt.slide_layouts[6]  



# for slide in ppt.slides:
for adjectif in adjectifs:
  # Ajoute un adjectif aléatoire sur le slide
  # adjectif = random.choice(adjectifs)

  # Attaching slide obj to slide
  slide = ppt.slides.add_slide(blank_slide_layout)
  # For adjusting the  Margins in inches
  left = top = width = height = Inches(1)
  # creating textBox
  txBox = slide.shapes.add_textbox(left, top, width, height)
  # creating textFrames
  tf = txBox.text_frame
  tf.text = adjectif
  paragraph = tf.paragraphs[0]
  paragraph.font.size = Pt(60)
  paragraph.font.bold = True
  paragraph.vertical_anchor = MSO_ANCHOR.MIDDLE
  paragraph.alignment = PP_ALIGN.CENTER
  # tf.fit_text(font_family="Calibri", max_size=60, bold=True)

  # Center the Text Frame : all solutions below were unsuccessful
  # tf.x = slide.width / 2 - tf.width / 2
  # tf.y = slide.height / 2 - tf.height / 2
  # slide_width = slide.shape.width
  # slide_width = slide.shapes.width
  # slide_width = slide.dimensions.width
  # slide_width = slide.slide_area.width
  # slide_width = bounding_box.right - bounding_box.left
  # tf.left = slide_width / 2 - tf.width / 2
  # tf.top = slide.height / 2 - tf.height / 2

# save file 
ppt.save('50_Adjectifs.pptx') 

  

print("done") 

我尝试了这两个命令:

  paragraph.vertical_anchor = MSO_ANCHOR.MIDDLE
  paragraph.alignment = PP_ALIGN.CENTER
b

但是,这似乎只对移动文本框架内文本的位置有用,而对移动幻灯片中的文本框架本身没有用...

欢迎任何想法 谢谢

alignment python-pptx
1个回答
0
投票

在最初确定文本框的大小/位置时效果很好

from pptx import Presentation  
from pptx.util import Inches, Pt 
from pptx.enum.text import MSO_ANCHOR, PP_ALIGN
import random

adjectifs = ["grand", "petit", "beau", "laid", "joli", "moche", "intelligent", "stupide", "gentil", "méchant"]
random.shuffle(adjectifs)

ppt = Presentation()  
blank_slide_layout = ppt.slide_layouts[6]

slide_width = ppt.slide_width
slide_height = ppt.slide_height
margin = Inches(1)

for adjectif in adjectifs:
    slide = ppt.slides.add_slide(blank_slide_layout)
    left = margin
    top = margin
    width = slide_width - 2 * margin
    height = slide_height - 2 * margin

    txBox = slide.shapes.add_textbox(left, top, width, height)
    tf = txBox.text_frame
    tf.margin_top = 0
    tf.margin_bottom = 0
    tf.margin_left = 0
    tf.margin_right = 0
    tf.text = adjectif
    paragraph = tf.paragraphs[0]
    paragraph.font.size = Pt(60)
    paragraph.font.bold = True
    paragraph.alignment = PP_ALIGN.CENTER
    for paragraph in tf.paragraphs:
        paragraph.alignment = PP_ALIGN.CENTER
        paragraph.space_before = Pt(0)
        paragraph.space_after = Pt(0)

    # Ensure vertical centering
    tf.vertical_anchor = MSO_ANCHOR.MIDDLE

ppt.save('50_Adjectifs.pptx')
print("done")
© www.soinside.com 2019 - 2024. All rights reserved.