14

I try to read a cell value, say E5 and E5 in the excel sheet contains a formula '=(A29 - A2)'. I use the following code and it returns me 0.00 instead of the actual value 1.440408 . Is there a way to solve this? I want to print the correct value. Please help me with this. Thank you.

book = xlrd.open_workbook('Test.xlsx')
first_sheet = book.sheet_by_index(0)
particular_cell_value = (first_sheet.cell_value(4,4))
print(particular_cell_value)
4
  • Seems like in first_sheet.cell_value(2, 2) you taking the value of C3 (or D4 if arguments of cell_value is 1-based) cell?
    – neoascetic
    Commented Dec 23, 2013 at 4:21
  • I'm sorry I was using (4,4) only. I had changed it for testing and gave it wrong here.
    – Ambi
    Commented Dec 23, 2013 at 4:23
  • I wouldn't mind using any excel parser in Python to get a proper result. I tried openpyxl and win32comclient as well but neither of them return the result correctly. :-(
    – Ambi
    Commented Dec 23, 2013 at 7:40
  • 1
    +1 because i had the same problem a while ago and i've never found a solution
    – 6160
    Commented Dec 23, 2013 at 7:51

2 Answers 2

21

Excel files store formulas and values independently. If the files are saved directly by Excel, then Excel will write both. However, files generated by other software, such as xlwt, OpenPyXL, and XlsxWriter, only write the formula. This is because there is no guarantee that a third-party package will be able to evaluate Excel's built-in functions exactly the same way that Excel does. Thus the recommendation (from Microsoft itself, actually) is for third-party packages to write the formula, set the value to zero, and set the global "auto-recalculate" flag on (so if the file is opened in Excel, Excel will go ahead and calculate all formula results automatically).

XlsxWriter spells it out in its documentation for the write_formula() method:

XlsxWriter doesn’t calculate the value of a formula and instead stores the value 0 as the formula result. It then sets a global flag in the XLSX file to say that all formulas and functions should be recalculated when the file is opened. This is the method recommended in the Excel documentation and in general it works fine with spreadsheet applications. However, applications that don’t have a facility to calculate formulas, such as Excel Viewer, or some mobile applications will only display the 0 results.

It then goes on to tell you how to write the value and the formula together if you really want to put a value there. The value would be whatever you choose, which isn't necessarily the same value that Excel would calculate. As of this writing, neither xlwt nor OpenPyXL support this feature.

So, having said all that, when it comes to reading an Excel file, naturally xlrd is going to pick up 0, unless the result was also written at the same time. You will notice that xlrd always picks up the correct result when the file was last saved by Excel. It will also pick up the result (which may or may not be correct) if you wrote one using XlsxWriter.

2
  • You saved my day... Thanks!
    – dtar
    Commented Jun 28, 2020 at 19:08
  • 2
    opened the file in excel, saved it, now it works! thank you! Commented Feb 20, 2021 at 15:06
0

There is a difference between a cell with a value of zero and an empty cell. To test for this in openpyxl you have to check for TYPE_NULL and also check the value for None, since the TYPE_NULL and TYPE_STRING use the same value 's'. This seems like a bug to me. Perhaps openpyxl will chose a unique value for TYPE_NULL in the future.

def use_openpyxl():
    import openpyxl
    print ("Using openpyxl:")
    wb = openpyxl.load_workbook('cell_formula_test.xlsx')
    ws = wb.get_sheet_by_name('Sheet1')
    for row in ws.rows:
        for cell in row:
            if (cell.data_type == openpyxl.cell.Cell.TYPE_NULL) and (cell.value == None):
                continue
            print ("  %s:%s:%s" % (cell.address, cell.data_type, cell.value))

In xlrd the cell_type does have a unique type for empty cells, so the checking is more straight forward.

def use_xlrd():
    import xlrd
    print ("Using xlrd:")
    wb = xlrd.open_workbook('cell_formula_test.xlsx')
    ws = wb.sheet_by_name('Sheet1')
    for i in range(ws.nrows):
        for j in range(ws.ncols):
            if ws.cell_type(i, j) != xlrd.XL_CELL_EMPTY:
                print ("  (%d, %d):%s:%s" % (i, j, ws.cell_type(i, j), ws.cell_value(i, j)))
11
  • HI ChipJust, I could never print the cells that contain formulas because, the condition fails at the if condition itself. :-( Since the value it is detecting is 0.00, it doesn't print any damn cell that contains a formula :-(
    – Ambi
    Commented Dec 24, 2013 at 7:14
  • Oh, I see. There is a difference between an empty cell and a zero value. You can check that with the cell_type attribute, especially for xlrd. I will rework this example to show this.
    – ChipJust
    Commented Dec 24, 2013 at 21:59
  • No, the problem here is me cell doesn't have a zero value. My cell has a value like 14.2 or 8.5 or anything. The thing is the sheet is generated by an XlsxWriter and when I try to read it back using xlrd or openpyxl, I face an issue of not being able to read the cell which contains formula. I'm not able to resolve this issue.
    – Ambi
    Commented Dec 26, 2013 at 3:44
  • Can you post your code and data. Do you still have the issue when you create the spreadsheet manually? Perhaps you could tell me what the above code shows for your data?
    – ChipJust
    Commented Dec 27, 2013 at 23:31
  • Data: TimeStamp Events Time from Event 1 20178.55447 Event 1 0 Event 3 from Event 1 20178.64812 Event 2 0.093647 0.1671 20178.67671 Event 3 0.122242 Event N from Event 1 20178.72157 Event 4 0.1671 0.559553 20179.08555 Event 5 0.531082 20179.10126 Event 6 0.54679 20179.11403 Event 7 0.559553
    – Ambi
    Commented Dec 30, 2013 at 4:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.