Python模組學習之xlrd 讀取Excel時xlrd.open_workbook(filePath,formatting_info=True) 報錯:NotImplementedError: formatting_info=True not yet implemented

來源:互聯網
上載者:User

標籤: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的代碼就不能這麼寫了。

解決辦法
  1. 將 .xlsx格式的Excel另存新檔 .xls 格式;(PS:直接將 .xlsx檔案尾碼修改為 .xls 是不可行的。)
  2. 使用openpyxl庫來讀取xlsx配置表。

Python模組學習之xlrd 讀取Excel時xlrd.open_workbook(filePath,formatting_info=True) 報錯:NotImplementedError: formatting_info=True not yet implemented

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.