0

I'm a newbie in python and trying to do some excel operations with python. My goal is to open multiple password protected excel files (.xls, and all in one folder) at once. They have the same password.

I managed to open one file with the below code. I'm struggling to find a way to open multiple files in one folder.

import msoffcrypto
import io
import pandas as pd
import xlrd
import glob


temp = io.BytesIO()

with open("/PATH/Filename.xls", 'rb') as f:
    excel = msoffcrypto.OfficeFile(f)
    excel.load_key('password')
    excel.decrypt(temp)

df = pd.read_excel(temp)

del temp

print(df)

Freymi
  • 1
  • 1
  • Take a look https://stackoverflow.com/questions/3207219/how-do-i-list-all-files-of-a-directory for how to get a list of all the files in a directory (folder). Then you can loop through each item in this list, and open the files as you are now with `with open(path...)` – mrblue6 Apr 28 '23 at 18:56
  • Thank you marble6 will try that out. Can I just create a directory variable and pass that in the with open() bracket? – Freymi Apr 28 '23 at 19:17
  • So you would do something like ‘file_list = listdir(directory)’ then loop through that list like ‘for file in file_list:’ then you can do whatever you need with the files – mrblue6 Apr 29 '23 at 20:04

0 Answers0