python-pptx: ValueError: value must be in range 256 to 2147483647 inclusive, got 2147483648 - when adding slide
Hello and first of all, thank you for the great work on this library which helps me a ton ! I’m using this library in order to write presentations based on existing presentations that user upload. So from time to time, I discoverer surprising edge cases like this one.
Here’s the most reduced case that I could produce:
- here is the existing file that I use as input: _debug.pptx
- here is the code producing the issue:
from pathlib import Path
from pptx import Presentation
input_file = Path(__file__).parent / "_debug.pptx"
presentation = Presentation(input_file)
layout = presentation.slide_layouts[0]
for _i in range(4):
presentation.slides.add_slide(layout)
- here is the error:
Traceback (most recent call last):
File "H:\workspaces\xd2sketch\slidespeak-fastapi\backend\app\new_pptx_writer\_reproduce.py", line 9, in <module>
presentation.slides.add_slide(layout)
File "H:\workspaces\xd2sketch\slidespeak-fastapi\venv\Lib\site-packages\pptx\slide.py", line 283, in add_slide
self._sldIdLst.add_sldId(rId)
File "H:\workspaces\xd2sketch\slidespeak-fastapi\venv\Lib\site-packages\pptx\oxml\presentation.py", line 56, in add_sldId
return self._add_sldId(id=self._next_id, rId=rId)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "H:\workspaces\xd2sketch\slidespeak-fastapi\venv\Lib\site-packages\pptx\oxml\xmlchemy.py", line 303, in _add_child
setattr(child, key, value)
File "H:\workspaces\xd2sketch\slidespeak-fastapi\venv\Lib\site-packages\pptx\oxml\xmlchemy.py", line 268, in set_attr_value
str_value = self._simple_type.to_xml(value)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "H:\workspaces\xd2sketch\slidespeak-fastapi\venv\Lib\site-packages\pptx\oxml\simpletypes.py", line 26, in to_xml
cls.validate(value)
File "H:\workspaces\xd2sketch\slidespeak-fastapi\venv\Lib\site-packages\pptx\oxml\simpletypes.py", line 605, in validate
cls.validate_int_in_range(value, 256, 2147483647)
File "H:\workspaces\xd2sketch\slidespeak-fastapi\venv\Lib\site-packages\pptx\oxml\simpletypes.py", line 56, in validate_int_in_range
raise ValueError(
ValueError: value must be in range 256 to 2147483647 inclusive, got 2147483648
I investigated a bit and the issue happens here: https://github.com/scanny/python-pptx/blob/d043334b984736a7a2ade3fb6f9adcdd97b3e8f5/pptx/oxml/presentation.py#L67
What happens is that the value of id_str_lst in this case is ['2147483644'] and this serves as a base for next id calculation.
So ultimately, the next id ends up being over the maximal accepted value and produces the error.
I also saw that this part of the code was last modified here: https://github.com/scanny/python-pptx/commit/1fca0922891ee72dda72197a456ff37b494e2a58
So I tried monkey patching the method using the previous implementation and this fixes the issue:
def _next_id_patched(self) -> int:
id_str_lst = self.xpath("./p:sldId/@id")
used_ids = [int(id_str) for id_str in id_str_lst]
for n in range(256, 258 + len(used_ids)):
if n not in used_ids:
return n
CT_SlideIdList._next_id = property(_next_id_patched)
I’m wondering if there is maybe a way to mix both solutions to support even more cases like this one ? Or do you have any other idea about how to fix this (I can eventually help with the PR).
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Comments: 16 (7 by maintainers)
Commits related to this issue
- fix: #972 next-slide-id fails when max used https://github.com/scanny/python-pptx/issues/972 Naive "max + 1" slide-id allocation algorithm assumed that slide-ids were assigned from bottom up. Appare... — committed to scanny/python-pptx by scanny 2 years ago
- fix: #972 next-slide-id fails when max used https://github.com/scanny/python-pptx/issues/972 Naive "max + 1" slide-id allocation algorithm assumed that slide-ids were assigned from bottom up. Appare... — committed to scanny/python-pptx by scanny 2 years ago
- fix: #972 next-slide-id fails when max used https://github.com/scanny/python-pptx/issues/972 Naive "max + 1" slide-id allocation algorithm assumed that slide-ids were assigned from bottom up. Appare... — committed to scanny/python-pptx by scanny 2 years ago
i spent an hr trying to read the ID’s and found related reason is that if you have a lot of master slides then an old ppt can have indexes that go beyond the max limit. manual copy paste of slides from 1 ppt to another doesnt refresh the slide ID and you really need VBA code to reset the slide ID numbers. even if you only have 5 slides in the deck but 10 master slides the indexing gets screwed up. make sense?
In my case, I made sure to run the code posted here: https://github.com/scanny/python-pptx/issues/972#issuecomment-2109434288 to patch the library before I use it.
And then the error was gone.
Let’s keep it open @sasensi. We might add this in a later release and seems maybe easier for folks to find an open issue than a closed one if they’re seeing this problem 😃
It could potentially happen frequently, if you were adding thousands of slides for example.
The problem with cacheing values like that is that the XML is the actual authoritative source and the two could easily become out-of-sync. So there’s no alternative to reading in the list every time I’m afraid. For example where would you store the cached list? A class has module lifetime so if you loaded more than one presentation on a single run it would be wrong. And an instance like Slide or even Presentation is just a proxy. You can easily have more than one Slide instance pointing to the same slide element subtree in the XML. So there’s no reliable location for this sort of thing other than the XML.
Also, the fall-back algorithm (which is what requires sorting) would only be engaged when the
max()+1algorithm failed, which has taken these ten years to show up for the first time. So I don’t expect we’d get any complaints 😃