Restoring previous version in Python - python

I am a py script that writes data into excel file. I am running the latter script on VM that sometimes crashes and if at that moment py script was writing data to excel, it corrupts the excel file.
Current solution to fix corrupt files is to manually 'Restore Previous Version' from file properties.
However I would like to do this task with python. Is there any solution for this?

You can save a copy of the excel file before manipulating it. If your script crashes because of an exception then you can catch it and then restore the file. If it crashes because of something like a memory error then you still have the copy of the file as a backup.

Related

How to open an excel file in heroku?

I'm using heroku with streamlit to do excel automations. But I need to execute the excel formulas so I can get the values computed by them, and to do so I would need to open the excel file. As it is an RPA, I don't want to open the excel file myself, manually, I want the code to open it for me inside the system and execute the formulas, so the code opens the sheet with pandas or openpyxl and get the values provided by the formulas. As heroku runs on linux, OS is not an option. I've tried xlwings a long time ago and it did not work as well, but I'll study it and see if it works, but if you guys have some other recommendation for me, that works better than xlwings (or even something about xlwings usage), it would be really helpfull!
Thanks in advance!
I have already tried to use pandas and openpyxl data_only=True to get the values computed by formulas, but both only work once the excel file has been opened manually, executing all formulae. I want to simulate the manual opening of the excel file inside heroku, but without literally opening it, so formulas inside the file are executed.

Saving Excel files in Python to a OneDrive Directory and then error opening the file do it being used by Python

My Python code properly writes to Excel using the pandas pd.ExcelWriter function. The challenge I am having is that I am saving the file to a OneDrive directory and it won't let me open the file in Excel once the code is executed unless I close Python. Effectively, the Excel file is considered still be used by Python and therefore cannot get synced in OneDrive.
Any suggestions on how to deal with this?

Python generated file not showing up in working directory

I have a couple of Python scripts that are acting funny on my computer. A couple of colleagues use the same script and don't have any issues. Both scripts take a .csv as an input, make some transformations and then open a new .csv or Excel file.
One of them will open the new file, but it won't show up in the working directory; when you go to save the open file then it says the file already exists in the working directory. It allows you to replace it, but even after this is doesn't show up. A picture of that is below.
Here is the saving script for that:
result.to_csv('TypeMapping_filled_{}.csv'.format(str(datetime.date.today())), index=None, header=True)
os.startfile('TypeMapping_filled_{}.csv'.format(str(datetime.date.today())))
The other one will run the script, but the file won't open or show up in the directory.
Here is the saving script for that:
styled_output.to_excel(f'REFORMATTED_{file_name[:(len(file_name)-4)]}.xlsx', index=None, header=True, engine='openpyxl')
Any thoughts on what might be going on?

Python and Excel Add Ins

I have written a code which opens an excel file via excel wings library and instruct such file to run a macro within the excel file itself. The macro within the file consists of a series of Refreshes which happen on another excel workbook through some excel add ins.
The issue is that when I open the excel file and run the VBA macro from there no issues arise. However, when I use Python to run the process I get a run time error 1004 in the VBA macro, which reads macro not available in the current workbook, despite the macro obviously being there. I believe this is because, for some reason, when Python launches Excel the add ins may not automatically load.
I have long looked for a solution around but nothing satisfactory yet. Any help is massively appreciated!

Windows scheduler with Python script that saves a file

I currently have a Python script that scrapes some data from the internet, then saves it as a pickle file. When running this from a terminal with python filename.py it saves correctly, since the Date Modified field of the pickle file changes. However, when running with the built-in scheduler it doesn't actually save the pickle file, since the Date Modified isn't changed, despite seeing the Python script being executed (terminal opens up and I see the script running).
I ticked the Run with highest privileges box in the scheduler, and despite that it doesn't save the pickle file. I thought it had to do with it not having write permission, but if it has the highest priviliges, surely it can save a file?
At the scheduled time a terminal opens, so I know it is actually being executed (print a message to make sure), but it doesn't show an error about the fact that it couldn't save the file or anything like that. The only reason I know it's not working is the Date Modified field not changing. How can I fix this?
Windows Task Scheduler has a default working directory of C:\Windows\System32. If you set a relative path to the file you are trying to write, it will likely be written into that directory. If you open a Command Prompt in the directory of your script and run it, the relative path will be that directory. So, you actually have two copies of the pickle file.
If you set an absolute path in your script to the file you want to write to, both methods of running your script will write to the same file.

Categories