How to access the real value of a cell using the openpyxl module for python

PythonCellOpenpyxl

Python Problem Overview


I am having real trouble with this, since the cell.value function returns the formula used for the cell, and I need to extract the result Excel provides after operating.

Thank you.


Ok, I think I ahve found a way around it; apparently to access cell.internal value you have to use the iter_rows() in your worksheet previously, which is a list of "RawCell".

for row in ws.iter_rows():

    for cell in row:

        print cell.internal_value

Python Solutions


Solution 1 - Python

Like Charlie Clark already suggest you can set data_only on True when you load your workbook:

from openpyxl import load_workbook

wb = load_workbook("file.xlsx", data_only=True)
sh = wb["Sheet_name"]
print(sh["x10"].value)

Good luck :)

Solution 2 - Python

From the code it looks like you're using the optimised reader: read_only=True. You can switch between extracting the formula and its result by using the data_only=True flag when opening the workbook.

internal_value was a private attribute that used to refer only to the (untyped) value that Excel uses, ie. numbers with an epoch in 1900 for dates as opposed to the Python form. It has been removed from the library since this question was first asked.

Solution 3 - Python

You can try following code.Just provide the excel file path and the location of the cell which value you need in terms of row number and column number below in below code.

    from openpyxl import Workbook
    wb = Workbook()
    Dest_filename = 'excel_file_path'
    ws=wb.active
    print(ws.cell(row=row_number, column=column_number).value)

Solution 4 - Python

Try to use cell.internal_value instead.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser3455972View Question on Stackoverflow
Solution 1 - PythonIulian StanaView Answer on Stackoverflow
Solution 2 - PythonCharlie ClarkView Answer on Stackoverflow
Solution 3 - PythonRohit GawasView Answer on Stackoverflow
Solution 4 - PythonVitaly IsaevView Answer on Stackoverflow