I have a ROM estimator tool I've made that takes data which is then output to an excel file with openpyxl. The problem I keep running into is when I output and XLOOKUP function to my excel file and go to open it, excel throws an error on the sheet where the formula is placed. When allowing excel to fix it, excel deletes the formula and I havent been able to find a work around.
I at first thought that the function was failing because of the order in which it was placed into the excel file. The XLOOKUP function populates a column of cells on the first page of the workbook. I attempted to use different functions that more of less did the same thing, but no matter the function I got the same "excel found a problem with one or more formula references in this worksheet". I also attempted to add the formula after the rest of the workbook was populated with information. If it was a data error maybe the data it was trying to access didn't exist yet. So I put the formula at the very end of my code, once everything else had been populated and I still got the same error message.
What doesn't make sense is that if I decide not populate the cell with the formula that is causing the issue using openpyxl and instead manually input the formula once the file is open and the rest of the worksheet is populated, it works totally fine. It just seems that when I input the formula i am unable to open the document without excel removing the formula to fix it.
Let me know if anyone has anything I could try to fix this issue. Right now i input the formula using openpyxl and simple remove the "=", adding it once I open it
Related
I'm working on an program which makes an excel file, then it gets the info into JSON and does more things. I'm struggling with Openpyxl. I found out today that if you don't open an Excel file made with Openpyxl with Excel, the formulas won't be computer.
So when I write:
excel = load_workbook(self.path_excel, read_only=True, data_only=True)
I don't get the formulas result, but only a "None" result. If I instead write data_only=False I will get my original formula. I very well know why this happens and I'm trying to find an automatic solution to open the excel file, compute all the formulas inside my excel file and close it. So when I open it up again in Openpyxl in the code after this "phase" I will have my results.
I'm using Python btw.
Here is the result I get and what I want to get:
1: data_only=True
data_only=False
What I really want with data_only=True
'delta_1': '12345' and more answers with numbers like when I open it in excel...
Thanks for the eventual help :)
I'm answering my own post for future reference and for others having my same problem...
Basically what worked was:
Stop using Pycharm as for some reason it was limiting my Python code;
Download Visual Studio Code;
Use the xlwings library as suggested by jezza_99 in the second comment.
Thanks for the help everyone :)
I've been looking for ages to find a suitable module to interact with excel, which needs to do the following:
Check a column of cells for an "incorrect" value and change it
Check for empty cells, and if so, replace it
Check a cell value is consistent with the contents of another cell(for example, if called Datasheet, the code in another cell = DS)and if not, change it.
I've looked at openpxyl but I am running Python 3 and I can only seem to find it working for 2.
I've seen a few others but they seem to be mainly focusing creating a new spreadsheet and simple writing/reading.
The Pandas library is amazing to work with excel files. It can read excel files easily and you then have access to a lot of tools. You can do all the operations you mentionned above. You can also save your result in the excel format
Apologies if this has been answered - I just spent the last hour or so looking for a specific method and was unable to find it.
I am getting data from a reporting program via keyboard emulation with pynput - the program has specific menus for copying data and selecting what is actually copied.
I have managed to get the data copied to the clipboard, and have then called openpyxl to load my selected workbook. What I can not figure out how to do is how to select a specific cell and then paste the data that I've already copied starting at that cell.
The copy parameters from the reporting program copy the data in a way that will paste into Excel properly (cell by cell) so I know that it will not try and paste all the data into one cell. I just can't determine the proper method to select the cell and paste it.
Thanks in advance for any help.
As a side note, I'm INCREDIBLY new to python - I am well versed in VBA but I am trying to branch out so I apologize if I've stated anything incorrectly.
Having the data already copied to the clipboard, you can use python pandas DataFrames to process it. Below is the method which accepts data copied from the clipboard and convert it into DataFrame.
pandas.read_clipboard(sep='\\s+', **kwargs)
Now you can select whichever column you want from the DataFrame and using the to_csv method, you can write to the Excel file.
DataFrame.to_csv()
Check out the documentation :
read_clipboard() - https://pandas.pydata.org/pandas-docs/stable/generated/pandas.read_clipboard.html
to_csv() - https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.to_csv.html
Sample Code :
import pandas as pd
data = pd.read_clipboard(sep='\\s+')
print(data)
I have a script to format a bunch of data and then push it into excel, where I can easily scrub the broken data, and do a bit more analysis.
As part of this I'm pushing quite a lot of data to excel, and want excel to do some of the legwork, so I'm putting a certain number of formulae into the sheet.
Most of these ("=AVERAGE(...)" "=A1+3" etc) work absolutely fine, but when I add the standard deviation ("=STDEV.P(...)" I get a name error when I open in excel 2013.
If I click in the cell within excel and hit (i.e. don't change anything within the cell), the cell re-calculates without the name error, so I'm a bit confused.
Is there anything extra that needs to be done to get this to work?
Has anyone else had any experience of this?
Thanks,
Will
--
I've investigated further and this is the issue:
When saving the formula "STDEV.P" openpyxl saves it as:
"=_xludf.STDEV.P(...)"
which is correct for many formula, but not this one.
The result should be:
"=_xlfn.STDEV.P(...)"
When I explicitly change the function to the latter, it works as expected.
I'll file a bug report, so hopefully this is done automatically in the future.
I suspect that there might be a subtle difference in what you think you need to write as the formula and what is actually required. openpyxl itself does nothing with the formula, not even check it. You can investigate this by comparing two files (one from openpyxl, one from Excel) with ostensibly the same formula. The difference might be simple – using "." for decimals and "," as a separator between values even if English isn't the language – or it could be that an additional feature is required: Microsoft has continued to extend the specification over the years.
Once you have some pointers please submit a bug report on the openpyxl issue tracker.
I'm trying to use OpenPyXL to
Open an .xlsx file
Read a cell that I know contains a number
Write a different number to that cell
Save the result to the same or different named .xlsx file
However even if I only perform the first and last steps the resulting .xlsx file has all formulas removed. The simplest version of my code goes like:
from openpyxl import load_workbook
wb = load_workbook(filename=file_path, data_only=False, guess_types=False)
wb.save(file_path_new)
However even without changing anything I still lose all the formulas. I have tried different values for the options. My biggest problem is that only yesterday, the full code (including reading and writing a numerical cell) was working and the saved result had the new number in that cell (when viewed in excel).
I updated from 1.8.5 to 2.0.2 at some point but can't remember if this was before or after the original code worked.