Openpyxl “Numbers Stored as Text”

被刻印的时光 ゝ 提交于 2020-01-14 10:38:10

问题


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.

Background: - New to Python (and coding in general) - 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 sheet, 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?

Any insight would be greatly appreciated!


回答1:


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.



来源:https://stackoverflow.com/questions/45242385/openpyxl-numbers-stored-as-text

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!