How to obtain sheet names from XLS files without loading the whole file?

后端 未结 6 646
独厮守ぢ
独厮守ぢ 2020-11-29 22:36

I\'m currently using pandas to read an Excel file and present its sheet names to the user, so he can select which sheet he would like to use. The problem is that the files a

6条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-29 22:40

    Python code adaptation with full pathlib path filename passed (e.g., ('c:\xml\file.xlsx')). From Dhwanil shah answer, without Django method used to create a temp dir.

    import xmltodict
    import shutil
    import zipfile
    
    
    def get_sheet_details(filename):
        sheets = []
        # Make a temporary directory with the file name
        directory_to_extract_to = (filename.with_suffix(''))
        directory_to_extract_to.mkdir(parents=True, exist_ok=True)
        # Extract the xlsx file as it is just a zip file
        zip_ref = zipfile.ZipFile(filename, 'r')
        zip_ref.extractall(directory_to_extract_to)
        zip_ref.close()
        # Open the workbook.xml which is very light and only has meta data, get sheets from it
        path_to_workbook = directory_to_extract_to / 'xl' / 'workbook.xml'
        with open(path_to_workbook, 'r') as f:
            xml = f.read()
            dictionary = xmltodict.parse(xml)
            for sheet in dictionary['workbook']['sheets']['sheet']:
                sheet_details = {
                    'id': sheet['@sheetId'],  # can be sheetId for some versions
                    'name': sheet['@name']  # can be name
                }
                sheets.append(sheet_details)
        # Delete the extracted files directory
        shutil.rmtree(directory_to_extract_to)
        return sheets
    

提交回复
热议问题