Post

[Error] Pandas로 엑셀 파일 로드 시 발생하는 오류 (진행 중)

파이썬에서 Pandas 라이브러리를 이용해 엑셀 파일을 로드할 때 가끔 아래와 같은 오류가 발생합니다.

1
ValueError: Excel file format cannot be determined, you must specify an engine manually.

해당 오류는 openpyxl이 xls 파일을 로드하지 못해서 발생하는 오류입니다.

아래 방법을 통해 엔진을 “xlrd”로 변경하여 해결할 수 있습니다.

1
2
3
pip install xlrd

df = pd.read_excel(PATH, engine='xlrd')

하지만 해당 코드로도 아래와 같은 오류가 발생하며 데이터가 로드되지 않는 경우가 있습니다.

1
XLRDError: Unsupported format, or corrupt file: Expected BOF record; found b'\r\n\r\n\r\n\r\n'

이때는 다음과 같은 방법을 통해 데이터의 확장자를 xlsx로 변경해주고 로드해야 합니다.

1
2
3
4
5
6
7
8
9
import win32com.client as win32

fname = PATH                            # Absolute path
excel = win32.gencache.EnsureDispatch("Excel.Application")
wb = excel.Workbooks.Open(fname)

wb.SaveAs(fname + 'x', FileFormat = 51) # FileFormat = 51 is for .xlsx extension
wb.Close()                              # FileFormat = 56 is for .xls extension
excel.Application.Quit()

하지만 아직도 해결이 되지 않는 경우가 있습니다.

해당 내용은 해결책을 찾으면 방법을 업데이트하도록 하겠습니다.

This post is licensed under CC BY 4.0 by the author.