问题
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