masaccio / numbers-parser

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

Unable to read image cell #43

Closed yifeikong closed 1 year ago

yifeikong commented 1 year ago

When I read a cell with an image, I got EmptyCell.

>>> table.cell("B2")
<numbers_parser.cell.EmptyCell object at 0x10e1b2b80>
masaccio commented 1 year ago

@yifeikong what were you hoping to extract from the cell? The image data is stored as pat of a cell format which isn't something I extract at the moment. It's possible I could find it, but I'd like to understand what data you want.

masaccio commented 1 year ago

What looks possible is that I implement a cell.image method that returns the image data if it's available

masaccio commented 1 year ago

The cell is still an EmptyCell since it contains no data. The image is a background. The API is:

table = doc.sheets[0].tables[0]
cell = table.cell("A1")
assert "PNG image data" in magic.from_buffer(cell.image_data)
assert len(cell.image_data) == 87857
assert "PNG image data" in cell.image_type
assert cell.image_filename == "pasted-image-17.png"
masaccio commented 1 year ago

Published as v3.9.1. See docs: https://github.com/masaccio/numbers-parser#cell-images

yifeikong commented 1 year ago

Hi, Thanks for the quick response! However, this does not work for my numbers file, details are as below:

(Pdb) l.
263             image_file_names = [x.file_name for x in datas if x.identifier == image_id]
264             if len(image_file_names) == 0:
265                 warn(f"No image found with ID {image_id}", UnsupportedWarning)
266                 return None
267  
268  ->         image_path_name = [
269                 x
270                 for x in self.model.objects.file_store.keys()
271                 if x == f"Data/{image_file_names[0]}"
272             ][0]
273             return (self.model.objects.file_store[image_path_name], image_file_names[0])
(Pdb) image_file_names
['Unknown-72.png']
(Pdb) self.model.objects.file_store.keys()
dict_keys(['Index/Document.iwa', 'Index/ViewState.iwa', 'Index/CalculationEngine-3611.iwa', 'Index/Tables/DataList-5689.iwa', 'Index/Tables/DataList-5690.iwa', 'Index/Tables/DataList-5694.iwa', 'Index/Tables/DataList-5696.iwa', 'Index/Tables/DataList-5699.iwa', 'Index/Tables/DataList-5703.iwa', 'Index/Tables/DataList-5704.iwa', 'Index/Tables/DataList-5706.iwa', 'Index/Tables/DataList-5707.iwa', 'Index/Tables/Tile-5708.iwa', 'Index/Tables/DataList-5709.iwa', 'Index/Tables/DataList-5712.iwa', 'Index/Tables/HeaderStorageBucket-5791.iwa', 'Index/Tables/HeaderStorageBucket-5802.iwa', 'Index/Tables/DataList-5806.iwa', 'Index/Tables/HeaderStorageBucket-5833.iwa', 'Index/Tables/HeaderStorageBucket-5838.iwa', 'Index/Tables/Tile-6285.iwa', 'Index/Tables/DataList-6286.iwa', 'Index/Tables/DataList-6287.iwa', 'Index/Tables/DataList-6288.iwa', 'Index/Tables/DataList-6289.iwa', 'Index/Tables/DataList-6290.iwa', 'Index/Tables/DataList-6291.iwa', 'Index/Tables/DataList-6292.iwa', 'Index/Tables/DataList-6293.iwa', 'Index/Tables/DataList-6294.iwa', 'Index/Tables/DataList-6295.iwa', 'Index/Tables/DataList-6296.iwa', 'Index/Tables/DataList-6345.iwa', 'Index/AnnotationAuthorStorage-3610.iwa', 'Index/DocumentStylesheet-3614.iwa', 'Index/DocumentMetadata.iwa', 'Index/Metadata.iwa'])

As you can see, the background image file was correctly found, but only the iwa files are contained in the file_store. I have confirmed that the image files are in the Data/ directory by looking at the contents of the numbers package.

yifeikong commented 1 year ago

After I change the file type to single file, it works! Thanks.