Excel hyperlink not working "cannot open the specified file" - python

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)

Related

I’m trying to get python to save as a user defined file name in a specific folder

I can get python to open an excel file and then save that same excel file, but that’s causing issues when I merge cells and then try to run the program again. I could resolve this issue if I can open the same excel template via python every time and then when the program is finished, it will save that excel file as a user defined name within the same folder the template was opened from. I know there is a saveAs function but I can’t find enough information to be able to get it to work. Thank you!

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

openpyxl corrupts spreadsheet if it contains a data source

I use openpyxl to interact with Excel files using Python 3.7. I open and save my .xlsx spreadsheets as follows:
from openpyxl import load_workbook
wb.load_workbook('file.xlsx', read_only=False)
wb.save('file.xlsx')
If file.xlsx contains no links to external data sources (such as SQL Server or Postgre-SQL), then there is no problem with the saved file and it opens okay in Excel after being processed by my Python script.
However, if file.xlsx does contain a link to external data, then upon executing the above script, the output file is now corrupted. When opening the file in Excel, the following error is reported and I have the option of attempting to recover it. When recovering, the data remains but all links to the data source are gone.
> We found a problem with some content in file.xlsx. Do you want us to try to recover as much as we can? If you trust the source of this workbook, click Yes.
It is easy to reproduce this error as follows:
Create a blank spreadsheet and save it as file.xlsx.
Run the above three lines of Python code to open and save the file. You will see this works fine and has no impact on the spreadsheet.
Now open file.xlsx in Excel and, from the Data tab, choose a data source. You can choose any data source (link to a csv file, a table within Excel, or an external data source - it doesn't matter).
Save the spreadsheet, then run the above Python script (which again, simply opens and saves it).
Open file.xlsx in Excel. You will see that it is now corrupted.
My conclusion is that, at the moment, openpyxl doesn't support spreadsheets that contain links to external data. It would be useful to have this confirmed, or for a workaround to the above issue to be proposed.
Thanks!!

Unlock password protected Workbook using VBA or 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.

Opening existing PowerPoint from a directory with Python 3.5

Open an existing PowerPoint Presentation from a directory
Example from here: Charts from Excel to PowerPoint with Python
My situation is different - the link showed how to to open a new presentation not a saved existing one.
error message:
module None.
You can call out to the open cmd and pass the filename to be opened as the only parameter. It will query the registry to find the program that should be used as default to open such a file, how to do it, and then do it.
This should work, did not check though
subprocess.run(['open', 'the file to be opened.pptx'])
Update: There is also https://docs.python.org/3/library/os.html#os.startfile

Categories

Resources