openpyxl Python Library Basics
-
openpyxl is python library to work with excel worksheets. Library supports both xlsx and xlsm formats.
This library enables us to do most of the actions that we do on an excel sheet using python code.
This includes creating and updating excel workbooks, create new sheets, add or modify content of a specified sheet, apply calculations, apply styles, add or remove rows or columns, etc.One thing to note is that if you are planning to use this for actual requirements, also install defusedxml to prevent quadratic blowup or billion laughs XML attacks.
Go through below topics to understand different usages of openpyxl. For more details, refer openpyxl site.
-
Install openpyxl
Download and install the latest version of python from Python Website.
Setting up virtual environment is not a must. But this is a best practice when working on python projects. Click virtual environment for more details.
Install openpyxl using the command: pip install openpyxl
-
To check if openpyxl installed successfully
Type command pip freeze
Should give an output like:et-xmlfile==1.1.0 openpyxl==3.0.9
-
Create new workbook
New excel files can be created using openpyxl. Import Workbook module from openpyxl library as below. Import os module also to work with the folders to save and access the file, if needed.
Copiedfrom openpyxl import Workbook w = Workbook() sh = w.active w.save('NewWorkbook.xlsx')
-
Open and read a workbook
To open an excel workbook, import load_workbook module and call load_workbook method with the file path as parameter as shown below. Worksheets can be accessed using zero based index.
Copiedfrom openpyxl import load_workbook w = load_workbook('NewWorkBook.xlsx') sh = w.worksheets[0] print(sh)
To modify a workbook, open it and add or remove content as needed and save as shown in create work book section.
-
Add content to a workbook
2 ways are there. Either add content to the specific cell or append content as new rows.
Copiedfrom openpyxl import Workbook w = Workbook() sh = w.active sh['A1'] = 'First row first cell' sh.append(["Second row first cell", 'cell 2', 3, 4, 'Cell 5']) w.save('NewWorkbook.xlsx')
-
Set column width
Shown below is an easy way to adjust the column width. Open the required sheet within the work book and using the column_dimensions property, we can set the width of any column.
Copiedfrom openpyxl import Workbook w = Workbook() sh = w.active sh['A1'] = 'First row first cell' colno = 1 sh.column_dimensions[str(chr(64 + colno))].width = 50 w.save('NewWorkbook.xlsx')
-
Align content
Cell content can be right, left or center aligned using the alignment property. To apply alignment, import Alignment module from openpyxl.styles. Alignment can be applied to columns as well.
Copiedfrom openpyxl import Workbook from openpyxl.styles import Alignment w = Workbook() sh = w.active sh['A1'] = 'First row first cell' colno = 1 sh.column_dimensions[str(chr(64 + colno))].width = 50 sh['A1'].alignment = Alignment(horizontal = 'right') w.save('NewWorkbook.xlsx')
-
Apply styles
Apply styles to a cell or an entire row as below. Import Font module from openpyxl.styles.
Copiedfrom openpyxl import Workbook from openpyxl.styles import Font w = Workbook() sh = w.active sh['A1'] = 'First row first cell' sh['A1'].font = Font(color = '15cafb15', bold=True) sh.append(["Second row first cell", 'cell 2', 3, 4, 'Cell 5']) for row in sh["2:2"]: #For entire second row row.font = Font(bold=True) w.save('NewWorkbook.xlsx')
-
Delete row
To delete a row, use the built-in delete_rows method and provide row number starting from 1. Multi-row delete option is also available.
Copiedfrom openpyxl import Workbook from openpyxl.styles import Font w = Workbook() sh = w.active sh.delete_rows(1) #Delete first row sh.delete_rows(2, sh.max_row+1) #Delete second to last row w.save('NewWorkbook.xlsx')
-
Add columns
To add a new column, just add some content to the required cells as shown below.
Copiedfrom openpyxl import Workbook from openpyxl.styles import Font w = Workbook() sh = w.active sh['C1'], sh['D1'] = 'Third column', 'Fourth column' w.save('NewWorkbook.xlsx')
-
Loop through rows
We can use iter_rows method to get all the rows with data. Use for loop to iterate through the rows.
Copiedfrom openpyxl import Workbook from openpyxl.styles import Font w = Workbook() sh = w.active sh['A1'], sh['B1'] = 20, 100 sh['A2'], sh['B2'] = 30, 50 for r in sh.iter_rows(): print(r[0].value, r[1].value) w.save('NewWorkbook.xlsx')
-
Apply calculations
We have the option to apply formulas in excel sheet rows or columns. Same thing can be implemented using openpyxl as well. Access the required cells and apply the calculations as shown below
To apply same type of calculations to an entire column, use a for loop.
Copiedfrom openpyxl import Workbook from openpyxl.styles import Font w = Workbook() sh = w.active sh['A1'], sh['B1'] = 20, 100 sh['A2'], sh['B2'] = 30, 50 for i, r in enumerate(sh.iter_rows()): sh['C' + str(i + 1)] = r[0].value * r[1].value w.save('NewWorkbook.xlsx')
-
openpyxl Samples