Hi everyone,
I’m using Words for Python via .NET, and came into an issue I cannot seem to solve.
I would need to check if the text in a text box is overflowing, and if so, reduce font size until all text fits. I cannot alter the text box dimensions whatsoever.
I did not see any methods or properties to check if text is overflowing, so I thought about the following method:
- Store the box dimensions
- Fit shape to text
- Compare old vs new dimensions. If they grew, it means there was overflow.
The issue is that the dimensions do not update after I apply fit_shape_to_text, but the shape is correctly enlarged to fit the text, so it definitely changes dimensions.
Ideally I need to know if the text in the box is overflowing, but if that’s not possible, at least know how to make the dimensions update after fitting the shape to the text.
Here’s a minimal example that I’m running on the attached doc.
doc.docx (16.4 KB)
import aspose.words as aw
from pathlib import Path
def autofit(shape: aw.drawing.Shape) -> aw.drawing.Shape:
shape.text_box.fit_shape_to_text = True
shape.text_box.text_box_wrap_mode = aw.drawing.TextBoxWrapMode.NONE
return shape
file = Path(r"doc.docx")
doc = aw.Document(file.name)
SHAPE = aw.NodeType.SHAPE
IS_DEEP = True
shapes = doc.get_child_nodes(SHAPE, IS_DEEP)
shape = shapes[0].as_shape() # I'm picking the first and only shape in my example doc
autofit(shape)
# The following attributes did not change after calling autofit(shape)
# even though if I save the document with doc.save(r"doc2.docx") to
# a new document I see the shape was resized.
#
# shape.height
# shape.width
# shape.coord_size.height
# shape.coord_size.width
# shape.bounds.x
# shape.bounds.y
# shape.top
# shape.bottom
# shape.left
# shape.right
doc.save(r"doc2.docx")
# The box was resized so evidently the text overflows and dimensions changed
# Nevertheless none of the parameters reflect that
Any help is greatly appreciated since I’ve navigated through the docs both for python via .net and .net itself, and I see no answer to my question. I’m at a total loss here.
Thanks in advance.