'python-pptx: Any way to make text vertical?
Is there any way to control text direction in python-pptx?
What I want to do is make text in shapes vertical as shown in the image below.
I have searched the documentation for hints but found nothing.
One simple solution seems to be to make a horizontal textbox and rotate it 90 degrees:
my_shape.rotation = 90
This makes the entire shape vertical, but what I want to do is change the direction of the text within the shape (i.e. make a vertical text box).
For half-width characters, its behavior looks just the same as the vertical textbox. However, for full-width characters like Japanese ones, the solution above changes the orientation of each character and this is what I don't expect(shown in the right side of the image).
What I want is shown in the left side of the image. Note that the direction of the Japanese sentence is vertical, but the orientation of the characters in it is still kept.
This can be achieved easily by PowerPoint. You can insert a vertical textbox as shown in the image below. (Make sure that the horizontal textbox & the vertical textbox are distinguished in the PowerPoint UI.)
I want to do the same thing by python-pptx.
Solution 1:[1]
python-pptx
package doesn't expose a ready-to-set attribute for this. At the end, it generates an XML and if you know what you need to touch, you can edit the XML directly to achieve the end result. You can also unzip the excel file, check the output xml to know what needs to be changed.
Many formatting style like anchroing of text, autofitting, and aligning of the text is done on the <a:bodypr>
tag. It is defined in the CT_TextBodyProperties
. The text direction is controlled by vert
attribute and that also have some valid values which are defined in ST_TextVerticalType
.
Assuming that you have the text frame where you want to add the text:
text_frame.text = '<your text>'
You can follow the hierarchy, get the elements and update the attributes. Text frame will have txBody
which will further have bodyPr
. But it also has a property to directly access the bodyPr
.
text_frame._bodyPr.attrib.update({'vert': 'vert'})
Here we add vert
attribute with a value vert
which would rotate only text and not the actual shape. You can also experiment with other values defined in ST_TextVerticalType
.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |