4

I am scanning data through encrypted excel files with python. I would like to read the file content without opening excel. Here is my code. I usually use pandas to read files but pandas.read_excel do not allows to add password.

from xlrd import *
import win32com.client
import csv
import sys

xlApp = win32com.client.Dispatch("Excel.Application")
xlwb = xlApp.Workbooks.Open(path1+file_name, Password='password')

Thank you

Sylvain
  • 253
  • 2
  • 7
  • 18
  • 1
    This should get you started: http://stackoverflow.com/questions/36850716/from-password-protected-excel-file-to-python-object – Mohammad Athar Sep 05 '16 at 02:25

1 Answers1

1

Check and upvote if below lines help......

from xlrd import *
import win32com.client
import csv
import sys
import pandas as pd
from tempfile import NamedTemporaryFile

xlApp = win32com.client.Dispatch("Excel.Application")
filename,password = r'fullpath','password'

# Note this line from the question posted
xlwb = xlApp.Workbooks.Open(filename, False, True, None, password)

xlws = xlwb.Sheets(1) # index is from 1
print (xlws.Name)
print (xlws.Cells(1, 1)) # if you need cell values

f = NamedTemporaryFile(delete=False, suffix='.csv')
f.close()
os.unlink(f.name)  

xlCSVWindows = 0x17  # CSV file format, from enum XlFileFormat
xlwb.SaveAs(Filename=f.name, FileFormat=xlCSVWindows) # Save as CSV
df = pd.read_csv(f.name)  
print(df.head())
df.to_csv('myoutput.csv',index=False)
Hietsh Kumar
  • 1,197
  • 9
  • 17