简体   繁体   English

如何使用 openpyxl 获取所有列的名称?

[英]How to get the name of all columns using openpyxl?

I want to set the width of all columns in excel sheet.我想设置 excel 工作表中所有列的宽度。 I can set the specific column width by我可以通过设置特定的列宽

ws.column_dimensions['A'].width = 2.5

I tried to implement in this way.我试图以这种方式实现。

cell_range = ['A','B','C','D','E','F','G','H','I','J']

for col_names in cell_range:
   ws.column_dimensions[col_names].width = 2.5

How can I iterate through all the name of columns?如何遍历所有列的名称?

I don't have a good way of testing it, but would this work?我没有测试它的好方法,但这行得通吗?

from openpyxl.utils import get_column_letter

for col in ws.iter_cols():
    name = get_column_letter(col[0])
    ws.column_dimensions[name].width = 2.5

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM