ものづくりのブログ

うちのネコを題材にしたものづくりができたらいいなと思っていろいろ奮闘してます。

【python】pandas でエクセルを読み込もうとしたらエラーになったので対応をメモ

pandas で以下のようにエクセルを読み込もうとしたらエラーになってしまいました。

エラー

エラーのコード

以下の処理を実行したところエラーになりました。

import pandas as pd
df = pd.ExcelFile('./sample.xlsx')

エラーの内容

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-2-7e30d7ce828d> in <module>
      1 import pandas as pd
----> 2 df = pd.ExcelFile('./sample.xlsx')

~/.pyenv/versions/3.7.10/lib/python3.7/site-packages/pandas/io/excel/_base.py in __init__(self, path_or_buffer, engine, storage_options)
   1110             if ext != "xls" and xlrd_version >= "2":
   1111                 raise ValueError(
-> 1112                     f"Your version of xlrd is {xlrd_version}. In xlrd >= 2.0, "
   1113                     f"only the xls format is supported. Install openpyxl instead."
   1114                 )

ValueError: Your version of xlrd is 2.0.1. In xlrd >= 2.0, only the xls format is supported. Install openpyxl instead.

対応

openpyxlをインストール

openpyxl を pip でインストールする。

$ pip install openpyxl

コード修正

「engine='openpyxl'」を追加する。

import pandas as pd
df = pd.ExcelFile('./sample.xlsx', engine='openpyxl')