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.
Related
I do not have code specifics for this problem unfortunately, but using Python 2.7+openpyxl engine after writing to xlsx causes the excel worksheet to be initially opened with a lot of blank rows, which then gets fixed automatically after scrolling down and back up. Here are some pictures:
There isn't a problem with writing/reading the files with blank rows because the resulting dataframe always matches the amount of rows there are supposed to be, so I believe this is happening when writing to excel. I can't seem to find a similar question online, so I'm wondering if I need to go over my code again or if someone else has hopefully experienced/knows the problem I seem to be experiencing. Thanks!
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'm new to python programming and I'm developing code that writes the value of a known variable into an excel sheet. This variable is the result of the python code that I developed. It updates its value every time the code is executed so in the end of the code, I would like to save the variable to the excel file.
My problem is related to updating the excel: when I write a new row, it deletes the previous rows. I want to save all my results in that excel file, but I only manage to save the last result I get.
I have tried many things but none has given me what I wanted as a result.
I'm using python 2.7 with pandas but I was trying to write in the excel file with openpyxl.
Question: How can I achieve saving rows to excel without deleting the previous rows?
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'm trying to write a simple tool to work on .csv and .xlsx files. The idea is to read in data as .csv add some modifications and export to .xlsx with the modifications added. I've got everything working but when I try to write out to the .xlsx file (using openpyxl in Python) I'm having some trouble. When I open the document in either LibreOffice Calc or Gnumeric some fields aren't showing. The fields I'm having trouble with include formulas. When I click on them the formulas are correctly typed in the field, but nothing is shown in the field. If I retype a character in the formula it's evaluated and the value shows up.
I had a poke around in the underlying .xml of the worksheet and the cells obviously doesn't have a cache. But I also noticed that it didn't always have the right data type. I tried to fix this by explicitly setting the type to formula but no luck.
What I use now to set the cell is:
cell = ws.cell(column=i,row=j)
cell.set_explicit_value(value=val,data_type=cell.TYPE_FORMULA)