- Python-bloggers
- Data science news and tutorials — contributed by Python bloggers
- How to automatically adjust Excel column widths in openpyxl
- Related
- Recent Posts
- openpyxl.worksheet.dimensions module¶
- How to change or modify column width size in Openpyxl
- Installing openpyxl
- To change or modify column width size
- How to change or modify column width size in Openpyxl
- Installing openpyxl
- To change or modify column width size
Python-bloggers
Data science news and tutorials — contributed by Python bloggers
How to automatically adjust Excel column widths in openpyxl
Posted on May 22, 2023 by George Mount in Data science | 0 Comments
This article was first published on Stringfest Analytics , and kindly contributed to python-bloggers. (You can report issue about the content on this page here)
Want to share your content on python-bloggers? click here.
Excel’s popularity stems from its user-friendly data formatting and presentation capabilities. Take, for example, the nuisance of narrow columns. Resolving this issue in Excel is as simple as selecting the affected columns and double-clicking on any one of them:
Python does present unique strengths to Excel, but visual formatting of the data can be difficult. It’s still, however, possible with the help of openpyxl :
Open the autofit_example.xlsx workbook created in the previous notebook and you’ll see all the content in the columns is easily visible:
What Excel automation tasks have you found particularly difficult in Python? Have you thought about defining functions for any other typical tasks? Let me know in the comments.
Related
To leave a comment for the author, please follow the link and comment on their blog: Stringfest Analytics .
Daily news and tutorials about data-science with Python, contributed by bloggers. Stay updated:
—>
Recent Posts
openpyxl.worksheet.dimensions module¶
class openpyxl.worksheet.dimensions. ColumnDimension ( worksheet, index=’A’, width=13, bestFit=False, hidden=False, outlineLevel=0, outline_level=None, collapsed=False, style=None, min=None, max=None, customWidth=False, visible=None, auto_size=None ) [source] ¶
Information about the display properties of a column.
Aliases can be used when either the desired attribute name is not allowed or confusing in Python (eg. “type”) or a more descriptive name is desired (eg. “underline” for “u”)
Always true if there is a width for the column
Set boundaries for column definition
class openpyxl.worksheet.dimensions. Dimension ( index, hidden, outlineLevel, collapsed, worksheet, visible=True, style=None ) [source] ¶
Information about the display properties of a row or column.
Aliases can be used when either the desired attribute name is not allowed or confusing in Python (eg. “type”) or a more descriptive name is desired (eg. “underline” for “u”)
Aliases can be used when either the desired attribute name is not allowed or confusing in Python (eg. “type”) or a more descriptive name is desired (eg. “underline” for “u”)
class openpyxl.worksheet.dimensions. DimensionHolder ( worksheet, reference=’index’, default_factory=None ) [source] ¶
Allow columns to be grouped
allow grouping a range of consecutive rows or columns together
- start – first row or column to be grouped (mandatory)
- end – last row or column to be grouped (optional, default to start)
- outline_level – outline level
- hidden – should the group be hidden on workbook open or not
Information about the display properties of a row.
Always true if there is a style for the row
Always true if there is a height for the row
Aliases can be used when either the desired attribute name is not allowed or confusing in Python (eg. “type”) or a more descriptive name is desired (eg. “underline” for “u”)
Aliases can be used when either the desired attribute name is not allowed or confusing in Python (eg. “type”) or a more descriptive name is desired (eg. “underline” for “u”)
Aliases can be used when either the desired attribute name is not allowed or confusing in Python (eg. “type”) or a more descriptive name is desired (eg. “underline” for “u”)
class openpyxl.worksheet.dimensions. SheetDimension ( ref=None ) [source] ¶
tagname = ‘dimension’¶ class openpyxl.worksheet.dimensions. SheetFormatProperties ( baseColWidth=8, defaultColWidth=None, defaultRowHeight=15, customHeight=None, zeroHeight=None, thickTop=None, thickBottom=None, outlineLevelRow=None, outlineLevelCol=None ) [source] ¶
tagname = ‘sheetFormatPr’¶ thickBottom ¶
© Copyright 2010 — 2023, See AUTHORS Revision 4212e3e95a42 .
Versions latest stable 3.1.2 3.1.1 3.1.0 3.1 3.0 2.6 2.5.14 2.5 2.4 Downloads html On Read the Docs Project Home Builds Free document hosting provided by Read the Docs.
How to change or modify column width size in Openpyxl
Microsoft Excel is one of the most widely used tools today. Most software projects intend to use it for organizing data. Thus it becomes necessary to have easy ways to access Excel sheets without opening the application every time. Python provides you ways to operate on excel without directly opening the software, through the openpyxl library.
By now, you may be familiar with the various operations you can perform using openpyxl. ( iter_rows, fill cells with colors using openpyxl)
This tutorial will teach you how to modify column width size in openpyxl.
Installing openpyxl
First of all, you must make sure to install the openpyxl library. You can do the same by running the below command on your terminal.
To change or modify column width size
In order to change the column width size, you can make use of the column_dimesnsions method of the worksheet class.
Syntax: worksheet.column_dimensions[column name].width=size
Let us look into the same with example below.
Consider an existing excel file codespeedy.xlsx as shown below;
So, now let us change the column size of column A;
import openpyxl worksheet = openpyxl.load_workbook("codespeedy.xlsx") sheet = worksheet.active sheet.column_dimensions['A'].width = 20 worksheet.save("codespeedy1.xlsx")
As you can see, we have modified the column size of A to 20 and saved the file after modification as codespeedy1.xlsx.
Similarly, you can also modify the column width of many rows as shown;
import openpyxl worksheet = openpyxl.load_workbook("codespeedy.xlsx") sheet = worksheet.active sheet.column_dimensions['A'].width = 20 sheet.column_dimensions['C'].width = 20 sheet.column_dimensions['E'].width = 30 worksheet.save("codespeedy1.xlsx")
Well, isn’t it amazing how you can manage such significant changes with such simple, small lines of code? Well, that in itself is the beauty of Python.
How to change or modify column width size in Openpyxl
Microsoft Excel is one of the most widely used tools today. Most software projects intend to use it for organizing data. Thus it becomes necessary to have easy ways to access Excel sheets without opening the application every time. Python provides you ways to operate on excel without directly opening the software, through the openpyxl library.
By now, you may be familiar with the various operations you can perform using openpyxl. ( iter_rows, fill cells with colors using openpyxl)
This tutorial will teach you how to modify column width size in openpyxl.
Installing openpyxl
First of all, you must make sure to install the openpyxl library. You can do the same by running the below command on your terminal.
To change or modify column width size
In order to change the column width size, you can make use of the column_dimesnsions method of the worksheet class.
Syntax: worksheet.column_dimensions[column name].width=size
Let us look into the same with example below.
Consider an existing excel file codespeedy.xlsx as shown below;
So, now let us change the column size of column A;
import openpyxl worksheet = openpyxl.load_workbook("codespeedy.xlsx") sheet = worksheet.active sheet.column_dimensions['A'].width = 20 worksheet.save("codespeedy1.xlsx")
As you can see, we have modified the column size of A to 20 and saved the file after modification as codespeedy1.xlsx.
Similarly, you can also modify the column width of many rows as shown;
import openpyxl worksheet = openpyxl.load_workbook("codespeedy.xlsx") sheet = worksheet.active sheet.column_dimensions['A'].width = 20 sheet.column_dimensions['C'].width = 20 sheet.column_dimensions['E'].width = 30 worksheet.save("codespeedy1.xlsx")
Well, isn’t it amazing how you can manage such significant changes with such simple, small lines of code? Well, that in itself is the beauty of Python.