masaccio / numbers-parser

Python module for parsing Apple Numbers .numbers files
MIT License
201 stars 14 forks source link

Add the same image to different table faild #78

Closed Carl1996 closed 6 months ago

Carl1996 commented 6 months ago

Bug Description I tried to add the same style object (just set bg_image) to two different tables, and the script generated the numbers file without an error, but the numbers file could not be opened properly.

Code

from numbers_parser import Document,BackgroundImage

def test_add_bg_image(configurable_save_file):

    doc = Document()
    doc.sheets[0].add_table()
    table1 = doc.sheets[0].tables[0]
    table2 = doc.sheets[0].tables[1]

    image_filename = "cat.jpg"
    image_data = open(image_filename, mode="rb").read()
    bg_image = BackgroundImage(image_data, image_filename)
    cats_bg_style = doc.add_style(bg_image=bg_image)

    table1.write(0, 0, "cats", style=cats_bg_style)

    table2.write(0, 0, "cats", style=cats_bg_style)

    doc.save(configurable_save_file)

test_add_bg_image("test1.numbers")

In addition, for two style objects with different bg_image, the numbers file can be opened correctly.

from numbers_parser import Document,BackgroundImage

def test_add_bg_image(configurable_save_file):

    doc = Document()
    doc.sheets[0].add_table()
    table1 = doc.sheets[0].tables[0]
    table2 = doc.sheets[0].tables[1]

    image_filename = "cat.jpg"
    image_data = open(image_filename, mode="rb").read()
    bg_image = BackgroundImage(image_data, image_filename)
    cats_bg_style1 = doc.add_style(bg_image=bg_image)

    image_filename = "test.png"
    image_data = open(image_filename, mode="rb").read()
    bg_image = BackgroundImage(image_data, image_filename)
    cats_bg_style2 = doc.add_style(bg_image=bg_image)

    table1.write(0, 0, "cats", style=cats_bg_style1)

    table2.write(0, 0, "cats", style=cats_bg_style2)

    doc.save(configurable_save_file)

test_add_bg_image("test2.numbers")
masaccio commented 6 months ago

In Console, I see an assertion from Numbers like this before it crashes:

#Assert *** Assertion failure #0: -[TSPDataManager addData:] /Library/Caches/com.apple.xbs/Sources/iWorkDependenciesMacOS/shared/persistence/src/TSPDataManager.mm:1735 Should not have two TSPData instances with the same digest 99291ca00549136f04ff70d04c1ca7c26e53d37b: <private> (<private>) and <private> (<private>).

Which is certainly true. Would be good to confirm you see this too but I'll proceed with a fix that avoids this.

masaccio commented 6 months ago

Give v4.10.3 a try.