这里回答如何将单个单元格更改为文本格式。简而言之,我可以做
from openpyxl.styles import numbers
cell.number_format = numbers.FORMAT_TEXT
使用for循环,我可以将其扩展到多个单元格。
但是如何使所有单元格适应文本格式呢?例如,如果有人后来手动将日期如2022-04-27
放入单元格CD130000
,我怎么能确保格式将是文本和日期不会被excel自动更改?谢谢你的帮助!
您应该能够使用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()