본 포스팅에서는 암호화된 엑셀 파일을 읽기 위해 전처리를 진행하고 pandas를 활용하여 읽어보고자 한다.
암호화된 엑셀파일
간혹 엑셀파일을 접하다 보면 암호화되어 밑에 그림과 같이 암호를 입력해야 열리는 파일들이 있다.
이 파일들은 pandas로 바로 읽으려고 하면 "XLRDError: Can't find workbook in OLE2 compound document"와 같은 오류가 발생하여 데이터를 로드하지 못한다.
이를 위해 새로운 라이브러리를 설치하여 암호화된 파일을 읽어보자.
msoffcrypto-tool
"msoffcrypto-tool"을 설치하여 암호화된 워드 파일 혹은 엑셀 파일을 읽을 수 있는 형태로 변환시켜 준다.
아래 링크는 msoffcrypto-tool 라이브러리의 github이다.
GitHub - nolze/msoffcrypto-tool: Python tool and library for decrypting MS Office files with passwords or other keys
Python tool and library for decrypting MS Office files with passwords or other keys - GitHub - nolze/msoffcrypto-tool: Python tool and library for decrypting MS Office files with passwords or other...
github.com
해당 사이트에서의 설명으로는 암호화된 MS Office 파일을 해독하기 위한 Python 도구 및 라이브러리라고 한다.
설치는 간단하다. "pip install msoffcrypto-tool"를 입력하여 설치하자.
설치하였다면 다음과 같이 코드를 입력하자.
import msoffcrypto
import io
decrypted = io.BytesIO()
with open(excel_file, "rb") as f: # excel_file : 암호화된 파일 선언
file = msoffcrypto.OfficeFile(f)
file.load_key(password="12345") # password 인자로 비밀번호 입력
file.decrypt(decrypted)
df = pd.read_excel(decrypted)
위 코드에 "excel_file"에 암호화된 엑셀 파일을 넣고, "password"인자로 암호를 입력하면 된다.
마지막으로 decrypted를 pd.read_excel로 읽으면 끝이다.
'Python > Pandas' 카테고리의 다른 글
[Python] Pandas를 활용하여 xls 파일 읽기 (0) | 2023.02.09 |
---|---|
[Python] Pandas와 for 문으로 엑셀 데이터 밑으로 붙이기 (0) | 2023.02.04 |
[Python] pandas로 xlsx 파일과 csv 파일 읽기 (0) | 2023.02.02 |
[Python] pandas로 엑셀 읽고 저장하기 (0) | 2023.01.31 |
댓글