如何使Excel模板中的每个单元格使用OpenPyXl的文本格式?

发布于 2025-01-24 16:07:09 字数 436 浏览 3 评论 0原文

在这里回答如何将单个单元格更改为文本格式。简而言之,我可以

from openpyxl.styles import numbers

cell.number_format = numbers.FORMAT_TEXT

使用循环使用,可以将其扩展到多个单元。

但是,如何使所有细胞适应文本形式?例如,如果以后有人手动将日期放在2022-04-27中由Excel自动改变?

感谢您的帮助!

Here it is answered how to change a single cell to text format. In a nutshell, I can do

from openpyxl.styles import numbers

cell.number_format = numbers.FORMAT_TEXT

Using for loops I can extend this to multiple cells.

But how can I make all cells adapt text-format? E.g. if somebody later manually puts a date like 2022-04-27 into cell CD130000, how can I ensure that the format will be of text and the date won't get auto-changed by excel?

Thanks for your help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤蝉 2025-01-31 16:07:09

您应该能够使用XLWING

import xlwings as xw


wb = xw.Book('Book1.xlsx')
ws = wb.sheets('Sheet1')
### Either of the following two lines should set the whole sheet to 'Text'
ws.api.Cells.NumberFormat = '@'
ws.cells.number_format = '@'
wb.save()
wb.close()

You should be able to use Xlwings

import xlwings as xw


wb = xw.Book('Book1.xlsx')
ws = wb.sheets('Sheet1')
### Either of the following two lines should set the whole sheet to 'Text'
ws.api.Cells.NumberFormat = '@'
ws.cells.number_format = '@'
wb.save()
wb.close()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文