How to Get Text Bounding Box Using Aspose.Slides for Python?

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.

Thanks in advance!

Screenshot 2023-12-20 at 09.29.55.png (470.0 KB)

@adisa.bolic,
Thank you for posting the question.

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.

More examples:

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:

Shape width: 341.4545593261719 Paragraph rectangle width: 165.5

Am I doing something wrong here?

image.jpg (110.8 KB)

get_rect_example.zip (27.6 KB)

@adisa.bolic,
I am working on the issue and will get back to you soon.

@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:

Shape width: 341.4545593261719
Paragraph rectangle width: 311.5

We recommend that you use the latest version of Aspose.Slides for Python.

Hmm I just upgraded Aspose.Slides to 23.12 (I had 23.9 before), and I still get the same result as before.

Here is a dump of my whole env:

Package                  Version             
------------------------ --------------------
appdirs                  1.4.4
Aspose.Slides            23.12.0
attrs                    19.3.0
Automat                  0.8.0
blinker                  1.4
cached-property          1.5.1
cachetools               5.3.1
certifi                  2023.7.22
chardet                  3.0.4
charset-normalizer       3.3.0
click                    8.1.7
cloud-init               23.1.2
colorama                 0.4.3
command-not-found        0.3
configobj                5.0.6
constantly               15.1.0
contourpy                1.1.1
cryptography             2.8
cycler                   0.12.1
dbus-python              1.2.16
distro                   1.4.0
distro-info              0.23ubuntu1
docker                   4.1.0
docker-compose           1.25.0
docker-pycreds           0.4.0
dockerpty                0.4.1
docopt                   0.6.2
entrypoints              0.3
filelock                 3.12.4
fonttools                4.43.1
fsspec                   2023.9.2
gitdb                    4.0.10
GitPython                3.1.37
google-api-core          2.12.0
google-auth              2.23.3
google-cloud-core        2.3.3
google-cloud-storage     2.11.0
google-crc32c            1.5.0
google-resumable-media   2.6.0
googleapis-common-protos 1.60.0
graphviz                 0.20.1
httplib2                 0.14.0
hyperlink                19.0.0
idna                     3.4
importlib-metadata       1.5.0
incremental              16.10.1
Jinja2                   2.10.1
jsonpatch                1.22
jsonpointer              2.0
jsonschema               3.2.0
keyring                  18.0.1
kiwisolver               1.4.5
language-selector        0.1
launchpadlib             1.10.13
lazr.restfulclient       0.14.2
lazr.uri                 1.0.3
lxml                     4.9.3
MarkupSafe               1.1.0
marshmallow              3.20.1
marshmallow-dataclass    8.6.0
matplotlib               3.8.0
more-itertools           4.2.0
mpmath                   1.3.0
mypy-extensions          1.0.0
netifaces                0.10.4
networkx                 3.2
numpy                    1.26.0
nvidia-cublas-cu12       12.1.3.1
nvidia-cuda-cupti-cu12   12.1.105
nvidia-cuda-nvrtc-cu12   12.1.105
nvidia-cuda-runtime-cu12 12.1.105
nvidia-cudnn-cu12        8.9.2.26
nvidia-cufft-cu12        11.0.2.54
nvidia-curand-cu12       10.3.2.106
nvidia-cusolver-cu12     11.4.5.107
nvidia-cusparse-cu12     12.1.0.106
nvidia-nccl-cu12         2.18.1
nvidia-nvjitlink-cu12    12.3.52
nvidia-nvtx-cu12         12.1.105
oauthlib                 3.1.0
packaging                23.2
pandas                   2.1.1
pathtools                0.1.2
pexpect                  4.6.0
Pillow                   9.5.0
pip                      23.3
protobuf                 4.24.4
psutil                   5.9.5
pyasn1                   0.5.0
pyasn1-modules           0.3.0
Pygments                 2.3.1
PyGObject                3.36.0
PyHamcrest               1.9.0
PyJWT                    1.7.1
pymacaroons              0.13.0
PyNaCl                   1.3.0
pyOpenSSL                19.0.0
pyparsing                3.1.1
pyrsistent               0.15.5
pyserial                 3.4
python-apt               2.0.1+ubuntu0.20.4.1
python-dateutil          2.8.2
python-debian            0.1.36ubuntu1
python-pptx              0.6.22
pytorch-warmup           0.1.1
pytz                     2023.3.post1
PyYAML                   6.0.1
requests                 2.31.0
requests-unixsocket      0.2.0
rsa                      4.9
scipy                    1.11.3
SecretStorage            2.3.1
sentry-sdk               1.32.0
service-identity         18.1.0
setproctitle             1.3.3
setuptools               65.5.0
shapely                  2.0.1
simplejson               3.16.0
six                      1.16.0
smmap                    5.0.1
sos                      4.4
ssh-import-id            5.10
sympy                    1.12
systemd-python           234
texttable                1.6.2
torch                    2.1.0
torch-scatter            2.1.2
tqdm                     4.66.1
triton                   2.1.0
Twisted                  18.9.0
typing_extensions        4.8.0
typing-inspect           0.9.0
tzdata                   2023.3
ubuntu-advantage-tools   8001
ufw                      0.36
unattended-upgrades      0.1
urllib3                  2.0.6
wadllib                  1.3.3
wandb                    0.15.12
websocket-client         0.53.0
wheel                    0.34.2
XlsxWriter               3.1.7
zipp                     1.0.0
zope.interface           4.7.1

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:

Package                  Version    
------------------------ ------------ 
appdirs                  1.4.4
Aspose.Slides            23.12.0
bezier                   2023.7.28
boltons                  23.0.0
brotlipy                 0.7.0
cachetools               5.3.1
certifi                  2023.7.22
cffi                     1.15.1
cfgv                     3.4.0
charset-normalizer       3.3.0
click                    8.1.7
colorama                 0.4.6
conda                    23.5.2
conda-content-trust      0.1.3
conda-libmamba-solver    23.5.0
conda-package-handling   2.1.0
conda_package_streaming  0.8.0
contourpy                1.1.1
cryptography             39.0.1
cycler                   0.12.1
distlib                  0.3.7
docker-pycreds           0.4.0
filelock                 3.12.4
fonttools                4.43.1
fsspec                   2023.10.0
gitdb                    4.0.10
GitPython                3.1.37
google-api-core          2.12.0
google-auth              2.23.3
google-cloud-core        2.3.3
google-cloud-storage     2.11.0
google-crc32c            1.5.0
google-resumable-media   2.6.0
googleapis-common-protos 1.60.0
graphviz                 0.20.1
identify                 2.5.30
idna                     3.4
Jinja2                   3.1.2
jsonpatch                1.32
jsonpointer              2.1
jstyleson                0.0.2
kiwisolver               1.4.5
libmambapy               1.4.1
lxml                     4.9.3
MarkupSafe               2.1.3
marshmallow              3.20.1
marshmallow-dataclass    8.6.0
matplotlib               3.8.0
menuinst                 1.4.19
mpmath                   1.3.0
mypy-extensions          1.0.0
networkx                 3.2
nodeenv                  1.8.0
numpy                    1.26.0
packaging                23.2
pandas                   2.1.1
pathlib2                 2.3.7.post1
pathtools                0.1.2
Pillow                   9.5.0
pip                      23.1.2
platformdirs             3.11.0
pluggy                   1.0.0
pre-commit               3.5.0
profile-viewer           0.1.5
protobuf                 4.24.4
psutil                   5.9.5
pyasn1                   0.5.0
pyasn1-modules           0.3.0
pycosat                  0.6.4
pycparser                2.21
pyOpenSSL                23.0.0
pyparsing                3.1.1
PySocks                  1.7.1
python-dateutil          2.8.2
python-pptx              0.6.22
pytz                     2023.3.post1
PyYAML                   6.0.1
requests                 2.31.0
rsa                      4.9
ruamel.yaml              0.17.21
RunSnakeRun              2.0.5
scipy                    1.11.3
sentry-sdk               1.32.0
setproctitle             1.3.3
setuptools               67.8.0
shapely                  2.0.1
six                      1.16.0
smmap                    5.0.1
snakeviz                 2.2.0
SquareMap                1.0.5
SquareMap3               1.0.8
sympy                    1.12
toolz                    0.12.0
torch                    2.1.0
tornado                  6.3.3
tqdm                     4.66.1
typing_extensions        4.8.0
typing-inspect           0.9.0
tzdata                   2023.3
urllib3                  2.0.6
virtualenv               20.24.6
wandb                    0.15.12
wheel                    0.38.4
win-inet-pton            1.1.0
wxPython                 4.2.1
XlsxWriter               3.1.7
zstandard                0.19.0

@adisa.bolic,
Thank you for the details. Could you please specify the version of Ubuntu you used?

Sure, it is Ubuntu 20.04.5 LTS.

@adisa.bolic,
Thank you for the information. I am working on the issue and will get back to you soon.

1 Like

@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.

The issues you found earlier (filed as SLIDESPYNET-148) have been fixed in Aspose.Slides for Python 24.4 (Windows x86, Windows x64, Linux, macOS x86-x64, macOS ARM64).
You can check all fixes on the Release Notes page.
You can also find the latest version of our library on the Product Download page or PyPI.