簡體   English   中英

使用openpyxl更改整個excel表的列寬

[英]change the columns width of entire excel sheet using openpyxl

我想更改我的 excel 文件中多個工作表的所有列寬。 我在其他帖子中發現的是關於更改一列的寬度。 我怎樣才能做到這一點?

這是我嘗試過但失敗的方法:

 wb = openpyxl.load_workbook('my_file.xlsx')
    for sheet in wb.worksheets:
        for column in sheet.columns:
            sheet.column_dimensions[column].width = 50

嗨,您可以使用以下代碼來構建動態單元格調整大小,但也可以用於您的目的

column_widths = []
for row in data:
    for i, cell in enumerate(row):
        if len(column_widths) > i:
            if len(cell) > column_widths[i]:
                column_widths[i] = 50
        else:
            column_widths += [50]

for i, column_width in enumerate(column_widths):
    worksheet.column_dimensions[get_column_letter(i+1)].width = column_width

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM