You are on page 1of 7

EXCEL AUTOMATION WITH

OPENPYXL IN PYTHON

Umer Saeed
BSc Electrical Engineering, MS Data Science
Department of Information System
School of Business and Economics
University of Management & Technology, Lahore, Pakistan
Sr. RF Optimization and Planning Engineer
umersaeed1981@gmail.com

banoqabil.pk
objective
❑ This code is setting the horizontal and vertical alignment of the text in all cells in all sheets of the workbook
to be centered.

❑ Also discussed the other options such as;


▪ Horizontal
▪ Vertical
▪ WrapText
▪ textRotation

banoqabil.pk
Alignment

❑ The workbook file can be manipulated and modified using methods and properties provided by
the openpyxl library.

❑ This line of code uses the openpyxl library to load a Microsoft Excel workbook file named “ABC.xlsx"
into a variable wb.

banoqabil.pk
Alignment
❑ This code sets the alignment of each cell
in every worksheet of a given workbook
wb to center both horizontally and
vertically.
❑ Here are the possible values for horizontal and vertical
❑ The code loops through each worksheet in the alignment:
workbook, and then iterates through each row ▪ horizontal:
and cell in the sheet. For each cell, the • general: Align the text based on the data type
cell.alignment attribute is set to an instance of • left: Align the text to the left
the Alignment class from the openpyxl.styles • center: Align the text to the center
• right: Align the text to the right
module. • fill: Fill the cell with the text and align it horizontally
• justify: Align the text to both left and right margins
❑ The Alignment class takes two optional • centerContinuous: Center the text across a continuous range of cells
arguments, horizontal and vertical, which • distributed: Distribute the text evenly across the width of the cell
specify how the text in the cell should be ▪ vertical:
aligned. In this code, both horizontal and • top: Align the text to the top of the cell
• center: Align the text to the center of the cell
vertical alignments are set to 'center'.
• bottom: Align the text to the bottom of the cell
• justify: Align the text to both top and bottom margins
• distributed: Distribute the text evenly across the height of the cell

banoqabil.pk
wrapText

❑ The wrapText property in openpyxl.styles.Alignment controls whether text in a cell should wrap to
multiple lines or be displayed on a single line.

❑ If wrapText is set to True, the text in the cell will wrap to fit within the cell's width.

banoqabil.pk
textRotation

❑ The textRotation property of the Alignment class in the openpyxl.styles module allows you to
control the orientation of the text in a cell.

❑ By default, text is oriented horizontally, with each line of text appearing in a separate row. The
textRotation property allows you to change this orientation by rotating the text by a certain number
of degrees, which can be positive or negative. A positive angle will rotate the text counterclockwise,
while a negative angle will rotate the text clockwise.

❑ For example, setting textRotation=90 will rotate the text in the cell by 90 degrees, causing each line
of text to appear in a separate column instead of row. You can also set textRotation=-90 to achieve
the same effect but with clockwise rotation.

banoqabil.pk

You might also like