fix(pptx): report an unreadable chart instead of losing the presentation - #2510
Open
Lukas (L4XB) wants to merge 1 commit into
Open
Lukas (L4XB) wants to merge 1 commit into
Lukas (L4XB) wants to merge 1 commit into
Conversation
`_convert_chart_to_markdown` catches `ValueError` and returns the placeholder only when the message contains "unsupported plot type". Any other `ValueError` matches that clause, fails the `if`, and falls off the end of the function, so it returns `None` -- and the caller does `md_content += ...`, which raises `TypeError: can only concatenate str (not "NoneType") to str`. One chart the converter cannot read therefore costs the whole deck. The most ordinary trigger is a non-numeric point in a chart's numeric cache: an `#N/A` left behind by the linked worksheet makes python-pptx raise `ValueError: could not convert string to float: '#N/A'` from `series.values`, which it parses eagerly for the whole series. ``` FileConversionException: File conversion failed after 1 attempts: - PptxConverter threw TypeError with message: can only concatenate str (not "NoneType") to str ``` Both branches returned the same placeholder, so drop the special-cased `ValueError` clause; `except Exception` already says what was meant, and now there is no path out of the function that returns nothing. Read each series' values through a small helper as well. The categories and the series names survive an unreadable cache, so a chart with one `#N/A` keeps its table instead of collapsing to `[unsupported chart]`. A series whose values cannot be read renders the same empty cells a short series already renders.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
PptxConverter._convert_chart_to_markdownhas a branch that can return nothing,and the caller concatenates the result straight into the document:
A
ValueErrorwhose message is anything else matches the first clause, failsthe
if, and falls off the end of the function.except Exceptionis neverreached, because
except ValueErroralready matched. So the function returnsNone, andmd_content += self._convert_chart_to_markdown(shape.chart)raises.One chart the converter cannot read costs the entire presentation:
The trigger this is reachable through
A non-numeric point in a chart's numeric cache.
#N/Ais what a linkedworksheet leaves behind, and python-pptx parses the cache eagerly, so the whole
series raises:
Measured on a two-slide deck, chart on slide 1 and plain text on slide 2, with
one
<c:v>20.0</c:v>rewritten to<c:v>#N/A</c:v>:The title, the second slide and the rest of the deck have nothing to do with
that chart, and were being thrown away with it.
Change
Drop the special-cased
ValueErrorclause. Both branches returned the sameplaceholder string, so it never did anything except open the hole. What remains
is the
except Exceptionthat was already there, and now there is no path outof the function that returns nothing.
Read each series' values through a small helper. The categories and the
series names are still readable when the cache is not, so a chart with one
#N/Akeeps its table instead of collapsing to[unsupported chart]. A serieswhose values cannot be read renders the same empty cells a short series already
renders through
sv[idx] if idx < len(sv) else None, so the row shape isunchanged.
Not changed: how
Nonerenders in a cell.map(str, row)already prints it fora short series and that is outside this fix.
Tests
packages/markitdown/tests/test_pptx_chart.py, 5 tests: a readable chart isstill a table; an
#N/Apoint keeps the deck and the table's shape; a chartthat fails for another reason becomes
[unsupported chart]and the decksurvives; an unsupported plot type still becomes
[unsupported chart]; and theconverter never returns
None, since the caller concatenates it.Whole suite, unchanged either way apart from the new file:
black(the repo's pre-commit hook) clean on both files.