Working with Password Protected Excel Sheets in Python on Linux - python

The problem is pretty simple. Every week I receive a bunch of password protected excel files. I have to parse through them and write certain parts to a new file using Python. I am given the password for the files.
This was simple to handle when this was being done on Windows and I could just import win32com and use client.Dispatch. But we are now moving all our code to linux so no more win32com for me.
Is there a way to open and read data from a password protected excel sheet in python on linux?
I have been searching for simple way to open a password protected excel file but no luck. I also tried finding a way to just remove the password protection so I can use xlrd like I would on a file that is not password protected but no luck going that route either.
Any help would be most appreciated.

with libreoffice and unoconv
unoconv --password='p4ssw0rd' -f csv protectedFile.xls
and then parse the csv file. Or export to another xls if you need the formatting or want to torture yourself
N.B. Edited after accepted. (--password is the correct switch, not -p, as noted by #enharmonic)
I've recently had an easier time using xlsxunpass
java -jar ./xlsxunpass.jar protected.xlsx unprotected.xlsx 'p4ssw0rd'

Related

Python - Opening a Password Protected CSV to read/write

I have been using python for only about two months so I am still quite new to coding.
Recently, in work, I wrote a code which opens an existing CSV file, performs a few operations and spits out a new CSV file. That bit I am happy with.
But what I want to know is what can I do in terms of securing the document and still running the code to open it? For example, I want to password protect this CSV file but want to prompt the user for the password which will be the only way to open/read the file.
Can anyone point me in the right direction please?
The CSV file is simply a formatted text (.txt) file. So to protect the file there are a few approaches.
Save CSV file then change permissions on it using OS commands - Password Protecting Excel file using Python
zip the csv file with password. Unfortunately, "The builtin zipfile module does not support writing password-encrypted files (only reading). Either you could use pyminizip. Refer to Create password protected zip file Python

Avoid pop-ups in Excel while running code in Python

I want to be able to open an Excel document and start manipulating the data without seeing any pop-ups.
I think the pop-ups are the ones stopping my Excel file from opening successfully. Here are the pop-ups I am seeing at excel and I would like to automatically answer them instead of doing it manually. I found some answers online but not for my case.
The file format and extension of "xxx" don't match. The file could be
corrupted or unsafe. Unless you trust its source, don't open it. Do
you want to open it anyway?
option1: Yes , option2: No , option3: Help
or
Open XML Please select how you would like to open this file:
As an XML table
As a read-only workbook
use the XML Source task pane
or
XML Import Error
ok
help
After I select: Yes & As an XML table & ok, everything works perfectly. If anyone could help me out I would much appreciate it.

Python: Edit a xlsx file open by another user

I looked at google for a solution based on python, but did not find any...
My python script is trying to edit an xlsx that might be opened by another user from MS excel.
If I try to overwrite the .xlsx file or the ~$*.xlsx file, I get a winError 32:
'process cannot access the file because it is being used by another process'
My problem is that users around me use MS excel to look at this output... And MS excel always lock the files that are open, by default.
It there a way to 'steal' the access from the other users. (As they are not editing it anyway).
I cannot not change the user permission (I think) as I am not admin of the files.
I am using windows 10.
Thanks for your advices.
cheers.
There really is no way around this - it is Excel preventing any other process on the system from obtaining write access.
If it were running on the same machine, you could consider connecting to the running Excel instance and getting it to close and reopen the document after opening it for writing yourself, but in your example it would likely be opened by someone on another machine.
The only solution here is to instruct your users to open the worksheet as read-only, which is an option every version of Excel allows, in which case you might be able to open it for writing. Whether that will allow you to update it while they are looking at it, is doubtful - you likely may want to look into connecting to an Excel sheet on OneDrive or SharePoint (or Teams etc. that use SharePoint as a back-end).

Recording unsaved Excel data with python or automsaving with vba

Using python, I want to continuously record some Excel calculations. The python/excel functions I have used will only read excel data from a saved spreadsheet. Is there a way to read unsaved data?
If not, is there a way to save excel and read the saved data periodically? I tried saving my sheets periodically (with a macro) but this is problematic if I am interacting with the spreadsheet during a save. Instead of saving my spreadsheet, excel saves a copy with a random name. If there is a way to remedy this (maybe some kind of vba error handling) that may solve the problem. Thanks.
Assuming you are using Excel on Windows and not OS X, you can try the win32com.client module, which you can get by installing the Python for Windows package:
http://sourceforge.net/projects/pywin32/
Unfortunately, the available documentation is pretty spotty at best... here's a start:
http://docs.activestate.com/activepython/3.3/pywin32/html/com/win32com/HTML/QuickStartClientCom.html
I should warn you that the COM API isn't very easy to use, so you are really better off sticking with VBA if your workflow requires Excel.
VBA error handling: http://www.cpearson.com/excel/errorhandling.htm

Excel Python API

Does anyone know of a way of accessing MS Excel from Python? Specifically I am looking to create new sheets and fill them with data, including formulae.
Preferably I would like to do this on Linux if possible, but can do it from in a VM if there is no other way.
xlwt and xlrd can read and write Excel files, without using Excel itself:
http://www.python-excel.org/
Long time after the original question, but last answer pushed it top of feed again. Others might benefit from my experience using python and excel.
I am using excel and python quite bit. Instead of using the xlrd, xlwt modules directly, I normally use pandas. I think pandas uses these modules as imports, but i find it much easier using the pandas provided framework to create and read the spreadsheets. Pandas's Dataframe structure is very "spreadsheet-like" and makes life a lot easier in my opinion.
The other option that I use (not in direct answer to your problem) is DataNitro. It allows you to use python directly within excel. Different use case, but you would use it where you would normally have to write VBA code in Excel.
there is Python library to read/write Excel 2007 xlsx/xlsm files http://pythonhosted.org/openpyxl/
I wrote python class that allows working with Excel via COM interface in Windows http://sourceforge.net/projects/excelcomforpython/
The class uses win32com to interact with Excel. You can use class directly or use it as example. A lot of options implemented like array formulas, conditional formatting, charts etc.
It's surely possible through the Excel object model via COM: just use win32com modules for Python. Can't remember more but I once controlled the Media Player through COM from Python. It was piece of cake.
Its actually very simple. You can actually run anything from any program. Just see a way to reach command prompt from that program. In case of Excel, create a user defined function by pressing Alt+F11 and paste the following code.
Function call_cmd()
Shell "CMD /C Notepad", vbNormalFocus
End Function
Now press ctrl+s and go back to Excel, select a cell and run the function =call_cmd(). Here I ran Notepad. In the same way, you can see where python.exe is installed and run it. If you want to pass any inputs to python, then save the cells as file in local directory as csv file and read them in python using os.system().

Categories