2

At the moment I need to get the actual width of the text, the best solution that I tried was to find the first line of text, and get its width. The presentations given to me were made by different people, and I cannot directly influence them. And it turns out that the shape frame itself in the presentation is often much wider than the text, which is a problem, because I need to get visible text shape collisions, which possible only when i have real frame of text(tried to show it in the screenshot1, screenshot2)

My best try to get real first line is:

# i have already compiled PIL font, with font typeface and size named "font"
# width of shape and text of shape
# and also class TextWrapper that wraps given string with given width and outputs a list

# get first line from wrapped text, and from tuple select width
width_first = font.getsize(TextWrapper(shape.text, font, width).text_lines[0])[0]

# get lines that wrapped at width first string
wrapped_lines = TextWrapper(shape.text, font, width_first).text_lines

# ... some calculations here 

Problem that a wrap symbol like '\n' not always be in text, but in presentation i see that wrap.

I tried to explain how I could, did anyone come across this at all?

EDIT:

I found some way how to do thig that i need. If u need something equals, see code.

import win32com.client

Application = win32com.client.Dispatch("PowerPoint.Application")
# WithWindow=False forces PowerPoint to do not open the PowerPoint Window
Presentation = Application.Presentations.Open("ABCPATH/to/presentation.pptx",
                                               WithWindow=False)

for Slide in Presentation.Slides:
    for Shape in Slide.Shapes:
        if Shape.HasTextFrame: # checks if shape has text, becouse we avoiding imgs
            # that what we need
            first_line = Shape.TextFrame.TextRange.Lines(1, 1)       

You must have installed PowerPoint Application to do that, and installed pywin32 And it works only on Windows, so thats not so good choose, but for me it works perfect

Maybe someone found this usable

newfox79
  • 21
  • 2

0 Answers0