0

I'm trying to call fit_text() on a textbox, but I keep getting this error:

'NoneType' object is not iterable

from this line

python3.6/site-packages/pptx/text/layout.py in _wrap_lines, line 112

which is

text, remainder = self._break_line(line_source, point_size)

This is my code which produces the error. I have no idea what's wrong here, neither line_source nor point_size are None.

def replace_text_of_shape(shape, data):
    if shape.has_text_frame:
        # replace_text_with(shape.text_frame.paragraphs, data)
        shape.text_frame.fit_text(font_family='Calibri', max_size=18, bold=False, italic=False)
Saleh
  • 173
  • 2
  • 12

3 Answers3

3

I received the same error recently. I'm not sure I have the best solutions but what I found is that the string(data) you are trying to save in your text frame is too big. I'm not sure if you are setting the bound on the text frame but you can either make it bigger by doing something like:(vary the size of obj_width)

txBox = slide.shapes.add_textbox(left=Inches(obj_left), top=Inches(obj_top), height=Inches(obj_height), width=Inches(obj_width))

Or you can make your variable "data" smaller. Not sure this is really the answer you want.

I ended up just putting a try/except around the fit_text command. See if it gives you something similar to what you are looking for.

variable1 = shape.text_frame
try:
    variable1.fit_text(font_family='Calibri', max_size=18, bold=False, italic=False)
except:
    print('could not fit the text correctly')
ttarter3
  • 83
  • 8
1

I just wanted to add that adding multiple try/except will give results very similar to the MSO_AUTO version (given you want to use as large a font as possible). Be warned it is very ugly, but it works.

try:
    text_frame.fit_text(font_family='Arial', max_size=55, bold=False, italic=False)
except TypeError:
    try:
        text_frame.fit_text(font_family='Arial', max_size=45, bold=False, italic=False)
    except TypeError:
        try:
            text_frame.fit_text(font_family='Arial', max_size=35, bold=False, italic=False)
        except TypeError:
            try:
                text_frame.fit_text(font_family='Arial', max_size=25, bold=False, italic=False)
            except TypeError:
                try:
                    text_frame.fit_text(font_family='Arial', max_size=15, bold=False, italic=False)
                except TypeError:
                    print('Could not fit text!')
garchompstomp
  • 120
  • 10
0

Slight variation on @garchompstomp 's answer:

for size in [55,45,35,25,15]:
    try:
        text_frame.fit_text(font_family='Arial', max_size=size, bold=False, italic=False)
        break
    except TypeError:
        pass    
    print('Could not fit text!')
Jeremy Bowyer
  • 142
  • 2
  • 5