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)
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'm designing a program in python tkinter that displays information that's currently in an excel spreadsheet. Ideally, I'd like to be able to share this program without needing to share the excel book as well. Is it possible to import the excel book into the program to make the program independent of that excel file? Let me know if I can provide any more clarification. Thank you!
You would need to assign the Excel file's content to a variable in your program to do so. I hope it isn't very large, but to make it easier I recommend:
saving your Excel file to .csv format
read it from Pytho
convert it to the data type you want (ex: string, list, ...)
save it back to a .txt file.
Now just copy the content of your .txt and assign that to a variable somewhere in your code.
Edit: what Alphy13 said is also a solution
Typically its best to create an example workbook that you can share. Give it all of the right parameters and leave out the sensitive data, or fill it in with fake data.
You could also set all of the variables that come from the excel file to have a default value that only changes when the workbook is present. This can be the first step toward creating proper error handling for your program.
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!
Assuming I have an excel sheet already open, make some changes in the file and use pd.read_excel to create a dataframe based on that sheet, I understand that the dataframe will only reflect the data in the last saved version of the excel file. I would have to save the sheet first in order for pandas dataframe to take into account the change.
Is there anyway for pandas or other python packages to read an opened excel file and be able to refresh its data real time (without saving or closing the file)?
Have you tried using mitosheet package? It doesn't answer your question directly, but it allows you working on pandas dataframes as you would do in excel sheets. In this way, you may edit the data on the fly as in excel and still get a pandas dataframe as a result (meanwhile generating the code to perform the same operations with python). Does this help?
There is no way to do this. The table is not saved to disk, so pandas can not read it from disk.
Be careful not to over-engineer, that being said:
Depending on your use case, if this is really needed, I could theoretically imagine a Robotic Process Automation like e.g. BluePrism, UiPath or PowerAutomate loading live data from Excel into a Python environment with a pandas DataFrame continuously and then changing it.
This use case would have to be a really important process though, otherwise licensing RPA is not worth it here.
df = pd.read_excel("path")
In variable explorer you can see the data if you run the program in SPYDER ide
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!