-1

代码:

import pandas as pd
file = "C:\\Users\\Len\\Desktop\\work\\student.xlsx"
file1 = "C:\\Users\\Len\\Desktop\\work\\temp.xlsm"
xl = pd.ExcelFile(file)
print(xl.sheet_names)
xlm = pd.ExcelFile(file1)
print(xlm.sheet_names)

目的是打印 Excel 文件 .xlsx 和 .xlsm 的工作表名称。但是,由于以下错误
,我无法获取文件的工作表名称:.xlsm

C:\Users\Len\AppData\Local\Programs\Python\Python310\python.exe D:/Coding/pythonProject/xl.py
['Report']
Traceback (most recent call last):
  File "D:\Coding\pythonProject\xl.py", line 7, in <module>
    xlm = pd.ExcelFile(file1)
  File "C:\Users\Len\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\io\excel\_base.py", line 1233, in __init__
    self._reader = self._engines[engine](self._io, storage_options=storage_options)
  File "C:\Users\Len\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\io\excel\_openpyxl.py", line 522, in __init__
    super().__init__(filepath_or_buffer, storage_options=storage_options)
  File "C:\Users\Len\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\io\excel\_base.py", line 420, in __init__
    self.book = self.load_workbook(self.handles.handle)
  File "C:\Users\Len\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\io\excel\_openpyxl.py", line 533, in load_workbook
    return load_workbook(
  File "C:\Users\Len\AppData\Local\Programs\Python\Python310\lib\site-packages\openpyxl\reader\excel.py", line 317, in load_workbook
    reader.read()
  File "C:\Users\Len\AppData\Local\Programs\Python\Python310\lib\site-packages\openpyxl\reader\excel.py", line 281, in read
    apply_stylesheet(self.archive, self.wb)
  File "C:\Users\Len\AppData\Local\Programs\Python\Python310\lib\site-packages\openpyxl\styles\stylesheet.py", line 198, in apply_stylesheet
    stylesheet = Stylesheet.from_tree(node)
  File "C:\Users\Len\AppData\Local\Programs\Python\Python310\lib\site-packages\openpyxl\styles\stylesheet.py", line 103, in from_tree
    return super(Stylesheet, cls).from_tree(node)
  File "C:\Users\Len\AppData\Local\Programs\Python\Python310\lib\site-packages\openpyxl\descriptors\serialisable.py", line 83, in from_tree
    obj = desc.from_tree(el)
  File "C:\Users\Len\AppData\Local\Programs\Python\Python310\lib\site-packages\openpyxl\descriptors\sequence.py", line 85, in from_tree
    return [self.expected_type.from_tree(el) for el in node]
  File "C:\Users\Len\AppData\Local\Programs\Python\Python310\lib\site-packages\openpyxl\descriptors\sequence.py", line 85, in <listcomp>
    return [self.expected_type.from_tree(el) for el in node]
  File "C:\Users\Len\AppData\Local\Programs\Python\Python310\lib\site-packages\openpyxl\styles\fills.py", line 64, in from_tree
    return PatternFill._from_tree(child)
  File "C:\Users\Len\AppData\Local\Programs\Python\Python310\lib\site-packages\openpyxl\styles\fills.py", line 102, in _from_tree
    return cls(**attrib)
TypeError: PatternFill.__init__() got an unexpected keyword argument 'extLst'
Process finished with exit code 1

帮我获取工作表名称。

4

1 回答 1

0

你需要做:

xlm = "C:\\Users\\Len\\Desktop\\work\\temp.xlsm"
xl = pd.read_excel(xlm, sheet_name=None)
Sheet_Name=xl.sheetnames

for i in Sheet_Name:
   print(i)

并不是

Print(xl.sheet_names)
于 2021-11-08T13:03:46.663 回答