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:

return max([255] + [int(id_str) for id_str in id_str_lst]) + 1

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: 1fca092

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).