Read from every sheet in a .xlsx in python only using standard libraries

Viewed 22

I have some code meant to read in all the data in an excel file into a list of dicts, however I wish to rewrite so that it can read in all the data in every sheet in the file, assuming every sheet is formatted the same. How would I go about this?

z = zipfile.ZipFile(fname)
strings = [el.text for e, el in iterparse(z.open('xl/sharedStrings.xml')) if el.tag.endswith('}t')]
rows = []
row = {}
value = ''
for e, el in iterparse(z.open('xl/worksheets/sheet1.xml')):
    if el.tag.endswith('}v'): 
        value = el.text
    if el.tag.endswith('}c'):                                  
        if el.attrib.get('t') == 's':
            value = strings[int(value)]
        letter = el.attrib['r']                          
        while letter[-1].isdigit():
            letter = letter[:-1]
        row[letter] = value
        value = ''
    if el.tag.endswith('}row'):
        rows.append(row)
        row = {}
return rows
0 Answers
Related