Unlock password protected Workbook using VBA or Python - python

I have a workbook name m.xlsx, but it's password protected and I've forgotten the password. How can I open it or un-protect it?
The following code does not work:
Unprotect workbook without password
I need the command to unprotect an Excel file from python
It ask for password when opened, and the above code does not unlock the workbook.
So, I want to put this code in a new workbook and then link my excel file with a code in place of THISWORKBOOK. Is there a way to put the m.xlsx file path without opening it (as opening of file needs password), and then run this code to unprotect the m.xlsx file?
Or is there any better way to unprotect workbook in VBA or Python programming? I have checked some Python code but they are opening the file and that is where the problem is.

Since the extension is xlsx it's the Microsoft Excel Open XML Format. That is essentially a .zip file.
Try this:
Copy the original file to a new folder so you don't tamper the original
Rename the file to a .zip extension
Extract the content with winzip or the like
In the extracted content, open xl\worksheets\sheet1.xml, preferably as a text file with notepad or the like
Search for <sheetProtection
Remove all from <sheetProtection to and including the terminating />
Save the content as a .zip
Rename to .xlsx
Password gone

Open with libreoffice and will not need a password. Get what you need and put in a new file.
I think is the best option.

Related

xlwings to write a text file locally

Hi I am looking to export data from the excel workbook out to my desktop as a text or an XML file.
I have it formed into an XML file and I am writing it locally when in DEBUG mode but when I add in the UDF and try run the same code it wont save the file.
Anyone any ideas as to why this wont work.
I needed to specify the Absolute Path for the creation of the file.
further information can be found here
xlwings calls a python function that should create a file, but no file get created

Exporting Python Data to Excel File on Sharepoint

I have a dataframe that I want to export to an existing Excel file that exists online on my company's sharepoint. I have the following line of code to do this:
df.to_excel(r'Path where the exported excel file will be stored\File Name.xlsx', sheet_name='Your sheet name', index = False)
The filepath starts with https and ends with a filetype .xlsx?web=1
Python of course doesn't recognise .xlsx?web=1 as a valid filetype so do I just ignore the ?web=1 at the end? This is what I did but get an access denied error. What do I need to do to be able to export to this file?
Edit: Filepath is https://companyname.sharepoint.com/sites/XXX/Shared%20Documents/07%20-%20LE%20(Loss%20Elimination)/02%20-%20Open%20(Current)%20Loss%20Elimantions/13%20-%20Material%20loss%20against%20BOM/Copy_Material%20Flow%20for%20HDW%20Euro%20When%20to%20FP.xlsx?web=1
The filepath starts with https and ends with a filetype .xlsx?web=1
That's not a path, that's a URL.
You cannot simply "save" to a URL.
There are Python libraries for working with sharepoint. For example Office365-REST-Python-Client, pysharepoint and this one on pypi, but the latter only seems to support downloading, not uploading.
Since pysharepoint is basically a front-end for SharePlum, you might want to look at that as well. It comes with documentation.

Excel hyperlink not working "cannot open the specified file"

I have a python app and I'm using xlwings to write to an Excel file. I am trying to create a link to another file. For now, I am trying to link to an Excel file. I am using the code:
ws.range(15, 8).value = '=HYPERLINK("C:\\file.xlsx")'
This creates a link but when I click the link I get the error "cannot open the specified file". The cell value is =HYPERLINK("C:\file.xlsx"). If I create a link to the same file using the "Insert Link" button in Excel it works and both cells show the same file path. Also I will need to create a link to a non-excel file that needs to be opened with a different program. How can I do this?
You should use the add_hyperlink method.
Example:
ws.range(15, 8).add_hyperlink("C:\\file.xlsx")
You need to specify a protocol - specifically, file.
Changing your link to file:///C:/file.xlxs would likely solve your problem. Non excel files would be opened with the default program for that file type (e.g. .txt would open notepad)

Error opening text file saved with .xls extension in python

I'm using labview to create and save data from an experiment. Labview itself creates a text file but saves it automatically with a .xls extension (word 1997-2003--it's an old setup that was never changed because it never broke). Whenever I go to open one of the data files, excel spits out this:
"The file you are trying to open, 'name.ext', is in a different format than specified by the file extension. Verify that the file is not corrupted and is from a trusted source before opening the file. Do you want to open the file now?"
I'm generating a lot of data, so I want to use python to sort it out and do some quick analysis over files in a directory.
The problem is that python doesn't like that it's a text file saved with a .xls extension. It can cycle through the directory just fine to get the file names, but whenver I actually try to open the file or do anything with it, I get the error in the image attached. This happens if I change the extension to .xls, .xlsx, or do nothing with it at all and let it try to open the original filename.
error message
I literally have hundreds of these .xls files. I know I can go through, open each one in excel and save as a real excel file by hand, but that will take hours. Can someone please help me figure a way around this error in python?
Dropbox Data File set
*Update. Matlab, when trying to read one of the files using xlsread, says this:
Error using xlsread (line 251)
File C:\Users\zane\Documents\Research Projects\PneuFish Project\Data\Nov 28 2016 ATI
Data\ATI_Data_2016Y_11M_28D_16h_36m_01s.xls not in Microsoft Excel Format.
Thank you!
You can use the module xlrd.
import xlrd
import csv
def csv_from_excel():
wb = xlrd.open_workbook('your_workbook.xls')
sh = wb.sheet_by_name('Sheet1')
your_csv_file = open('your_csv_file.csv', 'wb')
wr = csv.writer(your_csv_file, quoting=csv.QUOTE_ALL)
for rownum in xrange(sh.nrows):
wr.writerow(sh.row_values(rownum))
your_csv_file.close()
Taken from This Post
This will convert from .xls to .csv, which is easily manipulated with Python.
You've said that the file is a text file, so don't tell Python that it's an Excel file. Just use Python's open and read it as text, then do whatever you want with it. open doesn't care what extension a file has.
I'm going to guess that the format is actually tab-delimited. From memory, earlier versions of Excel would read in tab-delimited text files with the .xls extension without complaint, whereas csv files would always bring up the text import wizard, so this was a common dodge if saving data intended for Excel from a program that didn't support writing real Excel files.
If you want the LabVIEW code to write real Excel files in future, the Write to Measurement File express VI has an option to write in xlsx format. I'm not sure which version of LabVIEW first introduced this but it's been there for a few years now.

Python script for pasting values into .xls

I am creating a Python script to paste values pulled from another file into an Excel template.
The Excel template contains many macros that process the data it is given. This data is then sent to the clipboard, and the unsaved Excel file is then closed.
I have used a combo of xlwt and xlrd (it's a .xls file) to try and write to the Excel file, but it seems that these commands access the file without explicitly opening it (the Excel file never comes on screen).
I was curious if anyone could point me in the right direction of some tools that would allow me to explicitly open an Excel file (e.g. subprocess.call()), paste the data into some cells, then let the user do the rest.
Any help or criticism is appreciated.
EDIT:
I have been trying to use pywin32 for my purposes. It's not quite working. While it can successfully open the Excel file and run the macro, it cannot pass any values into the textbox of the macro.
Here is my current code:
import win32com.client as win32
import time
excel = win32.gencache.EnsureDispatch('Excel.Application')
wb = excel.Workbooks.Open(r'C:\Users\me\Desktop\file.xlsm')
excel.Visible = True
#if I run wb.Application.SendKeys(1) here, it places a 1 into a cell on the worksheet
wb.Application.Run('Set_Up_Sheet') #running the macro, which successfully launches
time.sleep(2)
wb.Application.SendKeys(1) #this seems to have no effect
How can I send values to this macro's textbox?

Categories

Resources