Search code examples
pythonopenpyxl

Reading a cell value as a str in openpyxl


I want to read a numeric cell value as a str rather than a float, but as far as I can see the API doesn't allow for this, is there something I'm missing?

The reason for this is the cell value is currency based, and as such I want to convert use it as a decimal not a float.

Alternatively, is there a way to get openpyxl to read the value directly as a decimal?


Solution

  • Excel uses IEEE 754 binary64 float format internally. Inside an xlsx file, sheet XMLs contain ordinary float string representations, like so:

    <c r="M11" s="592">
      <f>+K11*L11</f>
      <v>-3.6749999999999998</v>
    </c>
    

    Parsing this string as a float restores all accessible information perfectly. That said, float operations inside Excel lose accuracy, and there is no way to recover that precision without re-evaluating cell values or consulting external sources of data.