@@ -16,19 +16,21 @@ def temporary_directory(*args, **kwargs):
1616 shutil .rmtree (d )
1717
1818
19- def find_file_in_zip (zip , ext ):
19+ def find_file_in_zip (zip ):
2020 for filename in zip .namelist ():
21- if os .path .splitext (filename )[- 1 ].lower () == ext [:- 1 ]:
21+ try :
22+ ET .parse (zip .open (filename )).getroot ().tag in (
23+ 'workbook' , 'datasource' )
2224 return filename
25+ except ET .ParseError :
26+ # That's not an XML file by gosh
27+ pass
2328
2429
2530def get_xml_from_archive (filename ):
26- file_type = os .path .splitext (filename )[- 1 ].lower ()
27- with temporary_directory () as temp :
28- with zipfile .ZipFile (filename ) as zf :
29- zf .extractall (temp )
30- xml_file = find_file_in_zip (zf , file_type )
31- xml_tree = ET .parse (os .path .join (temp , xml_file ))
31+ with zipfile .ZipFile (filename ) as zf :
32+ xml_file = zf .open (find_file_in_zip (zf ))
33+ xml_tree = ET .parse (xml_file )
3234
3335 return xml_tree
3436
@@ -54,9 +56,8 @@ def save_into_archive(xml_tree, filename, new_filename=None):
5456
5557 # Extract to temp directory
5658 with temporary_directory () as temp_path :
57- file_type = os .path .splitext (filename )[- 1 ].lower ()
5859 with zipfile .ZipFile (filename ) as zf :
59- xml_file = find_file_in_zip (zf , file_type )
60+ xml_file = find_file_in_zip (zf )
6061 zf .extractall (temp_path )
6162 # Write the new version of the file to the temp directory
6263 xml_tree .write (os .path .join (
@@ -72,6 +73,3 @@ def _save_file(container_file, xml_tree, new_filename=None):
7273 save_into_archive (xml_tree , container_file , new_filename )
7374 else :
7475 xml_tree .write (container_file , encoding = "utf-8" , xml_declaration = True )
75-
76-
77-
0 commit comments