MartinPacker / md2pptx

Markdown To PowerPoint converter
MIT License
206 stars 31 forks source link

Special Formatting Not Working #142

Closed yamsu closed 1 year ago

yamsu commented 1 year ago

Hi, I'm unable to add superscript or subscripts in bullets and tables. I have updated the repo and tested on python 3.11 & 3.8 This is the markdown (Removed all meta options and templates). Incidentally, I get the same error with or without the metadata

# Presentation

### Special Formatting

* Bold : **Bold**
* italics : *italics*
* A<sub>6</sub>

This is the error:

Traceback (most recent call last):
  File ".../usr/bin/md2pptx", line 6525, in <module>
    slideNumber, slide, sequence = createSlide(prs, slideNumber, slideInfo)
  File ".../usr/bin/md2pptx", line 4199, in createSlide
    slide = createContentSlide(
  File ".../usr/bin/md2pptx", line 2613, in createContentSlide
    createListBlock(slideInfo, slide, renderingRectangle)
  File ".../usr/bin/md2pptx", line 2885, in createListBlock
    renderText(bulletsShape, slideInfo.bullets)
  File ".../usr/bin/md2pptx", line 1028, in renderText
    addFormattedText(p, bullet[1])
  File ".../usr/bin/md2pptx", line 1748, in addFormattedText
    set_subscript(font)
  File ".../usr/bin/md2pptx", line 794, in set_subscript
    if font.size < Pt(24):
TypeError: '<' not supported between instances of 'NoneType' and 'Pt'
MartinPacker commented 1 year ago

Investigating. I'm wondering how the font object came to not exist or else no have a size attribute.

Anyhow I've recreated this - thanks to your sample markdown, @yamsu.

MartinPacker commented 1 year ago

So we definitely have a font object passed into set_subscript - but it has no size attribute.

MartinPacker commented 1 year ago

And it has nothing to do with the previous bullets; It's all about the <sub> element. (Actually <sup> triggers the same error.)

MartinPacker commented 1 year ago

So it seems when python-pptx's add_run method is called - for a paragraph object - it doesn't set the font size for the new run. Normally that doesn't matter. In the case of handling <sub> and <sup> it matters because the font size is queried in add_subscript and add_superscript, respectively.

If the run's font size is less than 24 points the baseline is dropped / raised by one amount. If greater a larger baseline adjustment is done. (This all appears to be how Powerpoint itself does it.)

The solution for now is to test the run's font size. If it doesn't exist the code assumes the smaller baseline adjustment.

I've tested this and it works. I'll investigate documentation and push this out in the next hour - as 4.1.2. (There's another urgent fix - for card slides - I need to get out.)

MartinPacker commented 1 year ago

Documented (with additional explanations as to how <sub> and <sup> work) and released as v4.1.2.

Thanks for the bug report @yamsu!