How to convert Numbers stored as Text to Numbers in a specific cell, cell range, row, column

Hi, I use this python code to convert numbers that are stored as text to actual numbers

import jpype
import asposecells
jpype.startJVM()
from asposecells.api import Workbook

workbook = Workbook(insert Filepath)

workbook.getWorksheets().get(0).getCells().convertStringToNumericValue()

output.save(workbook)
jpype.shutdownJVM()

The problem is it converts all cells that have numbers stored as text in the worksheet to numbers. How do I convert it to just a specific cell, or a cell range, or rows or columns? Your assistance is much appreciated.

@myDAN
After obtaining a string type data for a certain cell, you can call Cell.putValue (stringValue, isConverted) to reset the value and set the second parameter to true for string to number conversion.

To perform a conversion operation within a certain range, you need to traverse the range and then set the values one by one.

1 Like

@myDAN,

Also, see the following lines where you can do it in one go i.e., obtaining the cell value and converting it to numeric value (if possible) while re-setting the cell value.

....
cells = workbook.getWorksheets().get(0).getCells()
cell = cells.get(0, 0)
cell.putValue(cell.getStringValue(), True)
1 Like

Thank you for the sample code amjad.sahi, this is exactly what I needed specially when using in a loop

@myDAN,

You are welcome, and it’s good to know that the suggested lines of code are helpful to you.