OpenPyXl用于循环的循环,该循环在指定的列中贯穿每个单元格,并用大写字母替换其值


for i in column_d: # column d is a tuple of cell objects in an Excel worksheet   
    if type(i.value) == str: #to avoid empty cells
        print(i.value) #cell value before
        i.value = i.value.upper() # is there a way to shorten this line?
        # without having to write i.value every time
        print(i.value + 'n') # cell value after

如果不将单元对象分配给变量,您可以更改其内容而无需调用"值",但是如果我只是将字符串重新分配到变量i。

的情况
<cell_object> = 'text' is the same as <cell_object>.value = 'text'
i = <cell_object>
i = i.value.upper()
type(i) # gives str

基本上是在问:有没有办法停止如此频繁地重复i.value?

for c in ws['D']:
    if c.data_type == "s" and c.value:
         c.value = c.value.upper()

检查属性具有微不足道的成本

最新更新