'How can I make every cell in an excel template be of text format with openpyxl?

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!



Solution 1:[1]

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()

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 moken