Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Openpyxl "Numbers Stored as Text"

Background:

  • Using Pycharm w/ Openpyxl
  • I work a lot in Excel and have been trying to automate some small daily tasks using python to increase my understanding of the language

Situation:

  • I am trying to modify an existing xlsx spreadsheet, and have been able to successfully change formatting like column width, zoom, etc.
  • However, I am really struggling with the dreaded "Numbers stored as text" scenario. There is a specific range of columns (Z:AZ) that always contain numbers, but they are stored as text by default.

Question:

  • Has anyone had experience converting text to numbers within an existing worksheet?

I've done some extensive searching on StackOverflow as well as many other sites and can't seem to find a solution that will work for me.

Any insight would be greatly appreciated!

like image 559
JSD Avatar asked Mar 08 '23 13:03

JSD


1 Answers

As you are getting Numbers Stored as Text error, I believe that the value you are adding in the Excel worksheet is String. Please confirm this by displaying type of your variable.

type(variable_name)

If it is <class'str'>, then add int while passing the variable to Excel sheet.

your_number = '412.5876'
ws['A1'] = float(your_number)

This should fix your problem.

like image 184
Rohit Avatar answered Mar 20 '23 06:03

Rohit