@@ -16,19 +16,21 @@ def temporary_directory(*args, **kwargs):
16
16
shutil .rmtree (d )
17
17
18
18
19
- def find_file_in_zip (zip , ext ):
19
+ def find_file_in_zip (zip ):
20
20
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' )
22
24
return filename
25
+ except ET .ParseError :
26
+ # That's not an XML file by gosh
27
+ pass
23
28
24
29
25
30
def 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 )
32
34
33
35
return xml_tree
34
36
@@ -54,9 +56,8 @@ def save_into_archive(xml_tree, filename, new_filename=None):
54
56
55
57
# Extract to temp directory
56
58
with temporary_directory () as temp_path :
57
- file_type = os .path .splitext (filename )[- 1 ].lower ()
58
59
with zipfile .ZipFile (filename ) as zf :
59
- xml_file = find_file_in_zip (zf , file_type )
60
+ xml_file = find_file_in_zip (zf )
60
61
zf .extractall (temp_path )
61
62
# Write the new version of the file to the temp directory
62
63
xml_tree .write (os .path .join (
@@ -72,6 +73,3 @@ def _save_file(container_file, xml_tree, new_filename=None):
72
73
save_into_archive (xml_tree , container_file , new_filename )
73
74
else :
74
75
xml_tree .write (container_file , encoding = "utf-8" , xml_declaration = True )
75
-
76
-
77
-
0 commit comments