Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python,带调整的openpyxl列_Python_Excel_Python 3.x_Openpyxl - Fatal编程技术网

Python,带调整的openpyxl列

Python,带调整的openpyxl列,python,excel,python-3.x,openpyxl,Python,Excel,Python 3.x,Openpyxl,希望有人能帮我解释一行代码。我是从另一个帖子上得到的,但无法使用评论提问,因为我是一个新用户,没有足够的信誉点 不管怎么说,这里是Python noob,请原谅我。海报如何知道列[0]。列将获得列名?我在openpyxl文档中找不到.column属性的来源 for col in worksheet.columns: max_length = 0 column = col[0].column # Get the column name for cell in col:

希望有人能帮我解释一行代码。我是从另一个帖子上得到的,但无法使用评论提问,因为我是一个新用户,没有足够的信誉点

不管怎么说,这里是Python noob,请原谅我。海报如何知道
列[0]。列
将获得列名?我在openpyxl文档中找不到
.column
属性的来源

for col in worksheet.columns:
     max_length = 0
     column = col[0].column # Get the column name
     for cell in col:
         try: # Necessary to avoid error on empty cells
             if len(str(cell.value)) > max_length:
                 max_length = len(cell.value)
         except:
             pass
     adjusted_width = (max_length + 2) * 1.2
     worksheet.column_dimensions[column].width = adjusted_width

列[0]。列在工作表中返回数字。列[column]只接受诸如“A”、“B”、“C”之类的字符来代替列。我已经修改了代码,现在可以正常工作了

import re

for col in _ws.columns:
max_lenght = 0
print(col[0])
col_name = re.findall('\w\d', str(col[0]))
col_name = col_name[0]
col_name = re.findall('\w', str(col_name))[0]
print(col_name)
for cell in col:
    try:
        if len(str(cell.value)) > max_lenght:
            max_lenght = len(cell.value)
    except:
        pass
adjusted_width = (max_lenght+2)
_ws.column_dimensions[col_name].width = adjusted_width

列[0]。列在工作表中返回数字。列[column]只接受诸如“A”、“B”、“C”之类的字符来代替列。我已经修改了代码,现在可以正常工作了

import re

for col in _ws.columns:
max_lenght = 0
print(col[0])
col_name = re.findall('\w\d', str(col[0]))
col_name = col_name[0]
col_name = re.findall('\w', str(col_name))[0]
print(col_name)
for cell in col:
    try:
        if len(str(cell.value)) > max_lenght:
            max_lenght = len(cell.value)
    except:
        pass
adjusted_width = (max_lenght+2)
_ws.column_dimensions[col_name].width = adjusted_width
“列[0]。列
是如何获得列名的?”文档说:
。列
此单元格的列号(基于1)。没有名称,但是给定单元格的索引
col[0]
。例如,列
B的
2
“列
col[0]是如何得到列名的?”文档说
。该单元格的列号(以1为基础)。没有名称,但是给定单元格的索引
col[0]
。例如,列
B
2