標籤:pre xxx AC call 修改 font 格式 XML work
問題:xlrd讀取Excel時加入 formatting_info=True 報錯
之前我們使用讀取xls檔案的時候都是使用的xlrd庫,但是這個庫只能操作 .xls格式,對於後來的 .xlsx的版本支援不算太好:
比如說:當你使用xlrd來載入 xlsx檔案的時候,在代碼中加入了
xlrd.open_workbook(filePath, formatting_info=True)
formatting_info=True 是用來儲存Excel原格式的。
馬上會得到這個報錯提示:
Traceback (most recent call last): File "xxxxxxxx\test_read_excel_color.py", line 7, in <module> xlrd.open_workbook(r'./xxxxx.xlsx',formatting_info=True) File "C:\Python27\lib\site-packages\xlrd\__init__.py", line 422, in open_workbook ragged_rows=ragged_rows, File "C:\Python27\lib\site-packages\xlrd\xlsx.py", line 751, in open_workbook_2007_xml raise NotImplementedError("formatting_info=True not yet implemented")NotImplementedError: formatting_info=True not yet implemented
官網中 formatting_info 的解釋是:
> formatting_info –The default is False, which saves memory. In this case, “Blank” cells, which are those with their own formatting information but no data, are treated as empty by ignoring the file’s BLANK and MULBLANK records. This cuts off any bottom or right “margin” of rows of empty or blank cells. Only cell_value() and cell_type() are available.
這個option使用與節約記憶體的。在這個情況下,空的儲存格,存在格式資訊但是沒有資料,將會被當成空來對待。這將會裁剪掉任何底部,右邊的“邊緣”空的表格。只有cell_value()和cell_type是有效。
實際上在當關閉了這個option之後,當程式需要去載入cell中的顏色代碼的時候將會存在下面的問題。
Traceback (most recent call last): File "xxxxx\test_read_execel_color1.py", line 10, in <module> xf_idx = xws1.cell_xf_index(0,0) File "C:\Python27\lib\site-packages\xlrd\sheet.py", line 420, in cell_xf_index self.req_fmt_info() File "C:\Python27\lib\site-packages\xlrd\sheet.py", line 1664, in req_fmt_info raise XLRDError("Feature requires open_workbook(..., formatting_info=True)")XLRDError: Feature requires open_workbook(..., formatting_info=True)
還不知道裡面是否還存在一些啥其他的問題。關閉了這個option之後,有些xlrd的代碼就不能這麼寫了。
解決辦法
- 將 .xlsx格式的Excel另存新檔 .xls 格式;(PS:直接將 .xlsx檔案尾碼修改為 .xls 是不可行的。)
- 使用openpyxl庫來讀取xlsx配置表。
Python模組學習之xlrd 讀取Excel時xlrd.open_workbook(filePath,formatting_info=True) 報錯:NotImplementedError: formatting_info=True not yet implemented