Skip to content

Fix crashes in multi-column detection on position-less shapes - #98

Open
eeshsaxena wants to merge 1 commit into
ssine:masterfrom
eeshsaxena:fix/multi-column-none-position
Open

Fix crashes in multi-column detection on position-less shapes#98
eeshsaxena wants to merge 1 commit into
ssine:masterfrom
eeshsaxena:fix/multi-column-none-position

Conversation

@eeshsaxena

Copy link
Copy Markdown

With --try-multi-column, converting a deck that contains a shape without an explicit position crashes.

python-pptx returns None for shape.top/left/width when the shape's XML has no offset (real .pptx files do this). multi_column.py then hits two crashes:

# is_two_column_text / assign_shapes:
sorted(ungroup_shapes(slide.shapes), key=attrgetter('top', 'left'))
# TypeError: '<' not supported between instances of 'NoneType' and 'Emu'

centroid_x = shape.left + shape.width / 2
# TypeError: unsupported operand type(s) for +: 'NoneType' and 'float'

Fixes:

  • Sort with a None-safe key (lambda s: (s.top or 0, s.left or 0)).
  • Skip shapes without a position/size when computing centroids (they have no horizontal centroid to contribute).
  • raise (ValueError, "...") (3 places) raises a tuple, which Python 3 rejects with TypeError: exceptions must derive from BaseException; changed to raise ValueError("...").

Added a regression test that builds a slide mixing positioned shapes with a position-less one; it crashes on master and passes with this change.

Shapes can have no explicit offset in their XML, so python-pptx returns None for
shape.top/left/width. is_two_column_text and assign_shapes then crashed:
sorting by attrgetter('top','left') raised TypeError comparing None with Emu,
and 'shape.left + shape.width / 2' raised TypeError on None. Use a None-safe
sort key and skip shapes without a position/size when computing centroids.

Also fix three 'raise (ValueError, msg)' statements, which raise a tuple rather
than the exception on Python 3.
@eeshsaxena

Copy link
Copy Markdown
Author

Hi! Gentle nudge on this one whenever you have some bandwidth. It's a small, self-contained fix (Fix crashes in multi-column detection on position-less shapes), and it's currently mergeable with no conflicts. No urgency at all, and I'm happy to make any changes you'd like. Thanks for maintaining pptx2md!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant