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

后端 未结 6 1567
夕颜
夕颜 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:47

    Updated version as of openpyxl 3.0.0 (using .columns fails with TypeError: expected :

    for column_cells in ws.columns:
        length = max(len(as_text(cell.value)) for cell in column_cells)
        ws.column_dimensions[column_cells[0].column_letter].width = length
    

提交回复
热议问题