Hi! I am interested whether it is possible to extract shape text bounding box positions in Python using Aspose? For example in the picture below I would like to get the position of the red box for each shape. Shape boxes usually are bigger than text bounding boxes (esp. for placeholder, text boxes, etc.) so that is why I can’t just grab the shape bounding box positions.
You can use the get_rect method from the Paragraph class to get rectangles of text bounds as follows:
with slides.Presentation("sample.pptx") as presentation:
for slide in presentation.slides:
for shape in slide.shapes:
if isinstance(shape, AutoShape):
for paragraph in shape.text_frame.paragraphs:
paragraph_rectangle = paragraph.get_rect()
# ...
I hope this will help you. Note: The rectangle’s coordinates are returned relative to the shape’s coordinates.
Thank you for the response, I have done some experimentation with the get_rect() function, and I can’t seem to make it work. For example, I attached a simple presentation with only one title shape and the code to extract the text rectangle for this shape. But, although the shape width and the text bounding box width should approx be the same, the text bounding box produced by get_rect() has only half the width. The output of the get_rect_example.py script I attached is:
@adisa.bolic,
Thank you for your patience. It looks like you are using an old version of Aspose.Slides for Python. With version 23.12, I get the following results:
I tried the same code with the same Aspose.Slides version (23.12) on a Windows machine instead of an Ubuntu that I was using before and now I get the correct output. Ultimately I do want to get this working on the Ubuntu, any idea what the difference may be?
Here is the env dump from the Windows machine:
@adisa.bolic,
I’ve reproduced a similar problem on Ubuntu.
We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.
Issue ID(s): SLIDESPYNET-148
You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.
@adisa.bolic,
Our developers have investigated the case.
The calculation of paragraph sizes is based on the calculation of the text size representing the given paragraph. In this case, the calculation of text size in Windows is based on the metrics of the Calibri Light font specified in the presentation. If the specified font is missing in Ubuntu, it is replaced with the most similar font, but this font has its own metrics different from the original ones. As a result, the calculation of paragraph sizes in different systems will lead to different results depending on the set of installed fonts.
To achieve the same result in Ubuntu as in Windows, it is necessary to have the same fonts (in this case, Calibri Light). To install this font in Ubuntu, copy the calibril.ttf file from Windows to Ubuntu to the .fonts folder and execute the command:
sudo fc-cache -f -v
After that, the results of measuring the paragraph sizes in Ubuntu and Windows will coincide.