简体   繁体   English

如何使用 Python PPTX 将上标/下标文本添加到 Powerpoint?

[英]How do I add superscript/subscript text to Powerpoint using Python PPTX?

I would like to add superscript and subscript text to powerpoint slides using python pptx.我想使用 python pptx 在 powerpoint 幻灯片中添加上标和下标文本。 When I try to change the superscript/subscript attributes no visual change occurs to the text on powerpoint.当我尝试更改上标/下标属性时,PowerPoint 上的文本不会发生视觉变化。 I have tried the following:我尝试了以下方法:

#Import modules
from pptx import Presentation
from pptx.enum.shapes import MSO_SHAPE
from pptx.util import Cm


#Open powerpoint file
prs = Presentation('filename.pptx')

#Add slide
slidelayout = prs.slide_layouts[0]
slide = prs.slides.add_slide(slidelayout)
shapes = slide.shapes

#Add a shape
shape = shapes.add_shape(MSO_SHAPE.ROUNDED_RECTANGLE, Cm(10), Cm(10), Cm(10), Cm(10))

#Add text
text_frame = shape.text_frame
paragraph = text_frame.add_paragraph()
run = paragraph.add_run()
run.text = 'this text should be superscript'
font = run.font
font.superscript = True
font.subscript = False

#Save the powerpoint file
prs.save('filename2.pptx')

This might help you:这可能会帮助您:

# Following functions are workarounds for python-pptx not having these functions for the font object
def set_subscript(font):
    font._element.set('baseline', '-25000')

def set_superscript(font):
    font._element.set('baseline', '30000')

def set_strikethrough(font):
    font._element.set('strike','sngStrike')

I have been able to copy in a superscript or shapes in text format from superscript generators like https://lingojam.com/TinyTextGenerator and then copy that into my string.我已经能够从https://lingojam.com/TinyTextGenerator等上标生成器以文本格式复制上标或形状,然后将其复制到我的字符串中。 I have used this in column names for tables.我在表格的列名中使用了它。 It shows up as a superscript in the code string when renaming the columns and also when output onto the powerpoint slide.在重命名列时以及在 output 到 powerpoint 幻灯片上时,它在代码字符串中显示为上标。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM