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

查看:42
本文介绍了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?

任何见解将不胜感激!

推荐答案

当您收到 Numbers Stored as Text 错误时,我相信您在 Excel 工作表中添加的值是 String.请通过显示变量的类型来确认这一点.

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天全站免登陆