Manage Superscript and Subscript in Python
Contents
[
Hide
]
You can add superscript and subscript text to any paragraph portion. In Aspose.Slides, use the escapement
property of the PortionFormat class to control this.
escapement
is a percentage from -100% to 100%:
- > 0 → superscript (e.g., 25% = slight raise; 100% = full superscript)
- 0 → baseline (no super/subscript)
- < 0 → subscript (e.g., -25% = slight lower; -100% = full subscript)
Steps:
- Create a Presentation and get a slide.
- Add a rectangle AutoShape and access its TextFrame.
- Clear existing paragraphs.
- For superscript: create a paragraph and a portion, set
portion.portion_format.escapement
to a value between 0 and 100, set text, and add the portion. - For subscript: create another paragraph and portion, set
escapement
to a value between -100 and 0, set text, and add the portion. - Save the presentation as PPTX.
import aspose.slides as slides
with slides.Presentation("pres.pptx") as presentation:
# Get a slide.
slide = presentation.slides[0]
# Create a text box.
shape = slide.shapes.add_auto_shape(slides.ShapeType.RECTANGLE, 100, 100, 200, 100)
shape.text_frame.paragraphs.clear()
# Create a paragraph for superscript text.
superscript_paragraph = slides.Paragraph()
# Create a text portion with regular text.
portion1 = slides.Portion()
portion1.text = "SlideTitle"
superscript_paragraph.portions.add(portion1)
# Create a text portion with superscript text.
superscript_portion = slides.Portion()
superscript_portion.portion_format.escapement = 30
superscript_portion.text = "TM"
superscript_paragraph.portions.add(superscript_portion)
# Create a paragraph for the subscript text.
subscript_paragraph = slides.Paragraph()
# Create a text portion with regular text.
portion2 = slides.Portion()
portion2.text = "a"
subscript_paragraph.portions.add(portion2)
# Create a text portion with subscript text.
subscript_portion = slides.Portion()
subscript_portion.portion_format.escapement = -25
subscript_portion.text = "i"
subscript_paragraph.portions.add(subscript_portion)
# Add the paragraphs to the text box.
shape.text_frame.paragraphs.add(superscript_paragraph)
shape.text_frame.paragraphs.add(subscript_paragraph)
presentation.save("TestOut.pptx", slides.export.SaveFormat.PPTX)