Python - Automatically adjust width of an excel file's columns

后端 未结 6 1551
夕颜
夕颜 2021-02-01 22:15

Newbie - I have a Python script that adjusts the width of different columns of an excel file, according to the values specified:

import openpyxl
from string impo         


        
6条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-02-01 22:42

    def auto_format_cell_width(ws):
        for letter in range(1,ws.max_column):
            maximum_value = 0
            for cell in ws[get_column_letter(letter)]:
                val_to_check = len(str(cell.value))
                if val_to_check > maximum_value:
                   maximum_value = val_to_check
            ws.column_dimensions[get_column_letter(letter)].width = maximum_value + 1
    

提交回复
热议问题