Search code examples
pythonexcelpandasopenpyxl

How to pass optimization options such as `read_only=True` to `pandas.read_excel` using `openpyxl` as the engine,?


I want to use pandas.read_excel to read an Excel file with the option engine="openpyxl". However, I also want to pass additional optimization options to openpyxl such as:

  • read_only=True
  • data_only=True
  • keep_links=False

How do I do this?


Solution

  • These are already implemented by default. From version 2.2:

    def load_workbook(
        self, filepath_or_buffer: FilePath | ReadBuffer[bytes], engine_kwargs
    ) -> Workbook:
        from openpyxl import load_workbook
    
        default_kwargs = {"read_only": True, "data_only": True, "keep_links": False}
    
        return load_workbook(
            filepath_or_buffer,
            **(default_kwargs | engine_kwargs),
        )