Adding Images
For the purpose of importing images inside our worksheet, we would be using openpyxl.drawing.image.Image. The method is a wrapper over PIL.Image method found in PIL (pillow) library. Due to which it is necessary for the PIL (pillow) library to be installed in order to use this method.
Image Used:
Example:
In this example, the openpyxl module is utilized to create a new Excel workbook. A row of data is added to the active sheet to distinguish it from the image. An image (“geek.jpg”) is then added to the worksheet using the openpyxl.drawing.image.Image class, and it is positioned at cell A2.
Python3
import openpyxl from openpyxl.drawing.image import Image wb = openpyxl.Workbook() sheet = wb.active sheet.append([ 10 , 2010 , "Geeks" , 4 , "life" ]) img = Image( "geek.jpg" ) sheet.add_image(img, 'A2' ) # Saving the workbook created wb.save( 'sample.xlsx' ) |
Output:
Refer to the below article to get detailed information about adding images.
Working with Excel Spreadsheets in Python
You all must have worked with Excel at some time in your life and must have felt the need to automate some repetitive or tedious task. Don’t worry in this tutorial we are going to learn about how to work with Excel using Python, or automating Excel using Python. We will be covering this with the help of the Openpyxl module and will also see how to get Python in Excel.