@@ -86,46 +86,45 @@ def handle_images(deps, archive: ZipFile) -> []:
8686
8787
8888def xlsx_embed_cells_images (buffer ) -> {}:
89- archive = ZipFile (buffer )
90- # 解析cellImage.xml文件
91- deps = get_dependents (archive , get_rels_path ("xl/cellimages.xml" ))
92- image_rel = handle_images (deps = deps , archive = archive )
93- # 工作表及其中图片ID
94- sheet_list = {}
95- for item in archive .namelist ():
96- if not item .startswith ('xl/worksheets/sheet' ):
97- continue
98- key = item .split ('/' )[- 1 ].split ('.' )[0 ].split ('sheet' )[- 1 ]
99- sheet_list [key ] = parse_element_sheet_xml (fromstring (archive .read (item )))
100- cell_images_xml = parse_element (fromstring (archive .read ("xl/cellimages.xml" )))
101- cell_images_rel = {}
102- for image in image_rel :
103- cell_images_rel [image .embed ] = image
104- for cnv , embed in cell_images_xml .items ():
105- cell_images_xml [cnv ] = cell_images_rel .get (embed )
106- result = {}
107- for key , img in cell_images_xml .items ():
108- all_cells = [
109- cell
110- for _sheet_id , sheet in sheet_list .items ()
111- if sheet is not None
112- for cell in sheet or []
113- ]
89+ with ZipFile (buffer ) as archive :
90+ # 解析cellImage.xml文件
91+ deps = get_dependents (archive , get_rels_path ("xl/cellimages.xml" ))
92+ image_rel = handle_images (deps = deps , archive = archive )
93+ # 工作表及其中图片ID
94+ sheet_list = {}
95+ for item in archive .namelist ():
96+ if not item .startswith ('xl/worksheets/sheet' ):
97+ continue
98+ key = item .split ('/' )[- 1 ].split ('.' )[0 ].split ('sheet' )[- 1 ]
99+ sheet_list [key ] = parse_element_sheet_xml (fromstring (archive .read (item )))
100+ cell_images_xml = parse_element (fromstring (archive .read ("xl/cellimages.xml" )))
101+ cell_images_rel = {}
102+ for image in image_rel :
103+ cell_images_rel [image .embed ] = image
104+ for cnv , embed in cell_images_xml .items ():
105+ cell_images_xml [cnv ] = cell_images_rel .get (embed )
106+ result = {}
107+ for key , img in cell_images_xml .items ():
108+ all_cells = [
109+ cell
110+ for _sheet_id , sheet in sheet_list .items ()
111+ if sheet is not None
112+ for cell in sheet or []
113+ ]
114114
115- image_excel_id_list = [
116- cell for cell in all_cells
117- if isinstance (cell , str ) and key in cell
118- ]
119- # print(key, img)
120- if img is None :
121- continue
122- if len (image_excel_id_list ) > 0 :
123- image_excel_id = image_excel_id_list [- 1 ]
124- with archive .open (img .target ) as f :
125- img_byte = io .BytesIO ()
126- im = PILImage .open (f ).convert ('RGB' )
127- im .save (img_byte , format = 'JPEG' )
128- image = File (id = uuid .uuid7 (), file_name = img .path , meta = {'debug' : False , 'content' : img_byte .getvalue ()})
129- result ['=' + image_excel_id ] = image
130- archive .close ()
115+ image_excel_id_list = [
116+ cell for cell in all_cells
117+ if isinstance (cell , str ) and key in cell
118+ ]
119+ # print(key, img)
120+ if img is None :
121+ continue
122+ if len (image_excel_id_list ) > 0 :
123+ image_excel_id = image_excel_id_list [- 1 ]
124+ with archive .open (img .target ) as f :
125+ img_byte = io .BytesIO ()
126+ im = PILImage .open (f ).convert ('RGB' )
127+ im .save (img_byte , format = 'JPEG' )
128+ image = File (id = uuid .uuid7 (), file_name = img .path , meta = {'debug' : False , 'content' : img_byte .getvalue ()})
129+ result ['=' + image_excel_id ] = image
131130 return result
0 commit comments