I am exporting data from python to excel using pandas in python. I am using a loop to iteratively create data frames and update the excel file.
I cannot use the writer.save() function in the middle of the loop because the next iteration has trouble opening the file to update it. The error is something about trying to open a closed file.
But if I use writer.save() at the end, and there is some error in the middle of the loop, then although an excel file is created (and updated until the error hits), I cannot open it because Windows cannot recognize the extension. This is preventing me from looking at the data that is being generated in the incomplete excel file.
Please help, thanks!
Related
I'm currently working on a project that reads from an Excel sheet with the openpyxl library, edits some of the data in the file, and then recreates a new one with the edited data.
My main issue is that when I save the new file, a lot of the excel functions I previously had are saved with curly brackets around them like this:
{=INDEX(M98:O98, MATCH(FALSE, ISBLANK(M98:O98),0))}
This is completely breaking the functionality of the excel sheet.
At first I wondered if this was an issue with how I was updating each individual cell. However, I commented out my entire function besides opening the file, and saving the new one, with no edits done, and I'm still getting the issue. The code now looks like this now:
def update(filename):
# Open excel sheet
e_workload = load_workbook(filename, data_only=False)
# Save results
e_workload.save(filename.replace(".xlsx", "_EDITED.xlsx"))
e_workload.close()
I'm just wondering what is causing this issue and how to fix it. I'm wondering if it's an issue with the library, but I don't want to rewrite my entire program without determining what the issue is first.
I am currently trying to develop a program, to use it I need to read data from an excel with Pandas.
The problem is that once I open Anaconda and Jupiter and run the program it won't let me go back to modify the excel it gets data from.
The program works and reads initial data, but I can't modify the excel sheet and save it for the program to run with other input data.
excel=pd.ExcelFile(r'C:\Users\ADURAN3\Anaconda3\python.xlsx')
df=pd.read_excel(excel,'Sheet1',index_col=0)
When I try to save the excel sheet with the new changes it forces me to rename it.
I would love it if you could help me, I am very new to pyhton.
Thank you very much.
To read an excel file, you don't need to do pd.ExcelFile(r'C:\Users\ADURAN3\Anaconda3\python.xlsx')
Just putting your file path into read_excel will work:
df = pd.read_excel(r'C:\Users\ADURAN3\Anaconda3\python.xlsx','Sheet1',index_col=0)
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!!
I'm using openpyxl to create a workbook in memory and fill it with data. Is there anyway to display that data in Excel at the end of the Python script without saving the file? It would be left up to the user to decide if they want to save the file or not. I'm guessing it's not possible but I wanted to see if I could get a more definitive answer here. Thanks!
I have another script that opens a bunch of excel sheets and exports a bunch of data to them. However, it is incapable of saving those documents automagically. Is there a way in python to grab all the currently open excel sheets, save them, and then close them?
What libraries are you using?
you can use
for each wb in xl.workbooks:
wb.close(true)