Openpyxl“数字存储为文本" [英] Openpyxl "Numbers Stored as Text"

查看:699
本文介绍了Openpyxl“数字存储为文本"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在StackOverflow以及许多其他站点上进行了广泛的搜索,但似乎找不到适合我的解决方案.

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.

背景: -Python的新手(和一般的编码) -使用带有Openpyxl的Pycharm -我在Excel中工作很多,一直在尝试使用python自动化一些日常任务,以增强我对该语言的了解

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

情况: -我正在尝试修改现有的xlsx工作表,并且已经能够成功更改格式,例如列宽,缩放等. -但是,我真的在可怕的数字存储为文本"方案中挣扎.在特定范围内的列(Z:AZ)中始终包含数字,但是默认情况下它们会存储为文本.

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?

任何见识将不胜感激!

推荐答案

当您将数字存储为文本错误时,我相信您在Excel工作表中添加的值是字符串. 请通过显示变量类型来确认这一点.

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)

如果它是<class'str'>,则在将变量传递到Excel工作表的同时添加int.

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.

这篇关于Openpyxl“数字存储为文本"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆