I don't think that named parameters work in this case. So you'd have to do something like:
xlwb = xlApp.Workbooks.Open(filename, False, True, None, password)
Install msoffcrypto-tool:
pip install msoffcrypto - tool
You could create an unencrypted version of the workbook from the command line:
msoffcrypto - tool Myfile.xlsx Myfile - decrypted.xlsx - p "caa team"
Or, you could use msoffcrypto-tool as a library. While you could write an unencrypted version to disk like above, you may prefer to create an decrypted in-memory file and pass this to your Python Excel library (openpyxl
, xlrd
, etc.).
import io import msoffcrypto import openpyxl decrypted_workbook = io.BytesIO() with open('Myfile.xlsx', 'rb') as file: office_file = msoffcrypto.OfficeFile(file) office_file.load_key(password = 'caa team') office_file.decrypt(decrypted_workbook) # `filename` can also be a file - like object. workbook = openpyxl.load_workbook(filename = decrypted_workbook)
Thank you so much for the great answers on this topic. Trying to collate all of it. My requirement was to open a bunch of password protected excel files ( all had same password ) so that I could do some more processing on those. Please find the code below.
import pandas as pd import os from xlrd import * import win32com.client as w3c import csv import sys from tempfile import NamedTemporaryFile df_list = [] # print(len(files)) for f in files: # print(f) if ('.xlsx' in f): xlwb = xlapp.Workbooks.Open('C:\\users\\files\\' + f, False, True, None, 'password') temp_f = NamedTemporaryFile(delete = False, suffix = '.csv') temp_f.close() os.unlink(temp_f.name) xlwb.SaveAs(Filename = temp_f.name, FileFormat = xlCSVWindows) df = pd.read_csv(temp_f.name, encoding = 'Latin-1') # Read that CSV from Pandas df.to_excel('C:\\users\\files\\password_removed\\' + f)
I'm trying to open a password protected file in excel without any user interaction. I searched online, and found this code which uses win32com.client When I run this, I still get the prompt to enter the password..., How to open a password protected excel file using... ,How to open write reserved excel file in python with win32com?, How to open write reserved excel file in python with win32com?
I'm trying to open a password protected file in excel without any user interaction. I searched online, and found this code which uses win32com.client When I run this, I still get the prompt to enter the password...
from xlrd
import *
import win32com.client
import csv
import sys
xlApp = win32com.client.Dispatch("Excel.Application")
print "Excel library version:", xlApp.Version
filename, password = r "\\HRA\Myfile.xlsx", 'caa team'
xlwb = xlApp.Workbooks.Open(filename, Password = password)
Install msoffcrypto-tool:
pip install msoffcrypto - tool
From the command line, you might make an unencrypted version of the workbook:
msoffcrypto - tool Myfile.xlsx Myfile - decrypted.xlsx - p "caa team"
To download the trial version of EasyXLS Excel Library, press the below button:,If you already own a license key, you may login and download EasyXLS from your account.,EasyXLS.dll must be added to your project. EasyXLS.dll can be found after installing EasyXLS, in "Dot NET version" folder.,EasyXLS.jar must be added to your classpath of the additional Java program. EasyXLS.jar can be found after installing EasyXLS, in "Lib" folder.
"" "-------------------------------------------------------------------------- Tutorial 27 This tutorial shows how to create an Excel file in Python and encrypt the Excel file by setting the password required for opening the file. -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --"" " import clr import gc clr.AddReference('EasyXLS') from EasyXLS import * print("Tutorial 27\n----------\n") # Create an instance of the class that exports Excel files, having two sheets workbook = ExcelDocument(2) # Set the sheet names workbook.easy_getSheetAt(0).setSheetName("First tab") workbook.easy_getSheetAt(1).setSheetName("Second tab") # Set the password for protecting the Excel file when the file is open workbook.easy_getOptions().setPasswordToOpen("password") # Export Excel file print("Writing file C:\\Samples\\Tutorial27 - protect Excel with password and encryption.xlsx.") workbook.easy_WriteXLSXFile("C:\\Samples\\Tutorial27 - protect Excel with password and encryption.xlsx") # Confirm export of Excel file sError = workbook.easy_getError() if sError == "": print("\nFile successfully created.\n\n") else: print("\nError encountered: " + sError + "\n\n") # Dispose memory gc.collect()
import py4j.GatewayServer;
public class GatewayServerApp {
public static void main(String[] args) {
GatewayServerApp app = new GatewayServerApp();
// app is now the gateway.entry_point
GatewayServer server = new GatewayServer(app);
server.start();
}
}
"" "-------------------------------------------------------------------------- Tutorial 27 This tutorial shows how to create an Excel file in Python and encrypt the Excel file by setting the password required for opening the file. -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --"" " import gc from py4j.java_gateway import JavaGateway from py4j.java_gateway import java_import gateway = JavaGateway() java_import(gateway.jvm, 'EasyXLS.*') print("Tutorial 27\n----------\n") # Create an instance of the class that exports Excel files, having two sheets workbook = gateway.jvm.ExcelDocument(2) # Set the sheet names workbook.easy_getSheetAt(0).setSheetName("First tab") workbook.easy_getSheetAt(1).setSheetName("Second tab") # Set the password for protecting the Excel file when the file is open workbook.easy_getOptions().setPasswordToOpen("password") # Export Excel file print("Writing file C:\\Samples\\Tutorial27 - protect Excel with password and encryption.xlsx.") workbook.easy_WriteXLSXFile("C:\\Samples\\Tutorial27 - protect Excel with password and encryption.xlsx") # Confirm export of Excel file sError = workbook.easy_getError() if sError == "": print("\nFile successfully created.\n") else: print("\nError encountered: " + sError + "\n\n") # Dispose memory gc.collect()
Updated: February 21, 2018
pd.read_excel(PATH)[...]
XLRDError: Can 't find workbook in OLE2 compound document
import pandas as pd
import xlwings as xw
PATH = '/Users/me/Desktop/xlwings_sample.xlsx'
wb = xw.Book(PATH)
sheet = wb.sheets['sample']
df = sheet['A1:C4'].options(pd.DataFrame, index = False, header = True).value
df