Microsoft Excel is a powerful data analysis and visualization tool that helps users create various charts, shapes, and other interactive elements. In some cases, we need to convert these visual elements into images for use in presentations, reports, or other documents. Exporting these elements manually can be more cumbersome, especially when dealing with multiple files. This article will explore how to automatically extract charts, shapes, and other elements from Excel using Python and save them as images, mainly covering the following content:
Why export charts, shapes, and other elements in Excel as pictures?
Converting charts, shapes, and other elements in Excel into pictures can bring many benefits, such as:
- Easy to share: Images can be easily embedded in emails, presentations, or other documents without the need for the recipient to install Excel.
- Consistent format: Images maintain the same appearance across environments compared to Excel files that may present different results on different devices.
- Performance improvement: Static images usually load faster than interactive elements.
- Enhanced security: The image format can prevent others from accessing or modifying underlying data and protect information security.
Tools and settings
To save charts, shapes, and other elements in Excel as pictures in Python, this article uses for Python. This library supports creating, manipulating, and converting Excel files in Python without relying on Microsoft Office or other software.
Install
There are two versions available, and you can choose the appropriate version according to your needs:
Community Edition: Free to use, but each .xls file is limited to 200 lines and 5 sheets (there is no such limit for the .xlsx file).
Installation command:
pip install
Commercial version: Complete features, but watermark will be added.
Installation command:
pip install
After the installation is complete, the library's API can be called in the Python application.
Python export Excel charts as images
Excel files can contain embedded charts (charts located within worksheets) or chart worksheets (standalone charts). Here is how to export both as pictures using Python.
Export the chart as an image
To save or export charts embedded in Excel files as pictures, refer to the following steps:
- Initialize the object of the Workbook class and load the Excel file using the LoadFromFile() method.
- Iterate through the worksheet in the workbook by the collection.
- Iterate through the charts in the current worksheet by the collection.
- Use the () method to save each chart as an image object.
- Save the image object as a picture file in the specified format (for example, PNG, JPEG).
Implement code
from import * def save_charts_as_images(excel_file, output_directory): """ Will specify Excel All charts in the file are exported as pictures,and save to the specified output directory。 parameter: excel_file (str): Contains the chart Excel File path。 output_directory (str): The destination directory path for image saving。 """ workbook = Workbook() (excel_file) for sheet_index in range(): sheet = [sheet_index] for i, chart in enumerate(): image = () image_path = f"{output_directory}/{}_chart_{i}.png" (image_path) () # Usage examplesave_charts_as_images("Example.xlsx", "Output Directory")
Export the chart sheet as an image
If you need to convert a chart sheet (standalone chart) in Excel to a picture, you can refer to the following steps:
- Initialize the object of the Workbook class and load the Excel file using the LoadFromFile() method.
- Iterate through the chart sheet in the Workbook object through the collection.
- Save each chart worksheet as an image object using the SaveChartAsImage() method of the Workbook object.
- Save the image object as a picture file in the specified format (for example, PNG, JPEG).
Implement code
from import * def save_chart_sheets_as_images(excel_file, output_directory): """ Will specify Excel All chart sheets in the file are exported as pictures,and save to the specified output directory。 parameter: excel_file (str): Contains chart worksheets Excel File path。 output_directory (str): The destination directory path for image saving。 """ workbook = Workbook() (excel_file) for i, chart_sheet in enumerate(): image = (chart_sheet) image_path = f"{output_directory}/Chart worksheet_{i}.png" (image_path) () # Usage examplesave_chart_sheets_as_images("Example.xlsx", "Output Directory")
Python exports shapes and other elements in Excel as images
In addition to pictures, you can also export a variety of Excel elements, such as geometric shapes, shape combinations, text boxes, check boxes, list boxes, combo boxes and radio buttons, as pictures. The specific steps are as follows:
- Initialize the object of the Workbook class and load the Excel file using the LoadFromFile() method.
- Iterate through the worksheet in the workbook.
- Iterates over the specified shape or element type in the current worksheet.
- Use the SaveToImage() method to save each shape or element as an image object.
- Save the image object as a picture file in the specified format (for example, PNG, JPEG).
Implement code
The following code shows how to export geometry in an Excel worksheet as a picture:
from import * def save_geometric_shapes_as_images(excel_file, output_directory): """ Will specify Excel All geometric shapes in the file are exported as pictures,and save to the specified output directory。 parameter: excel_file (str): Contains geometric shapes Excel File path。 output_directory (str): The destination directory path for image saving。 """ workbook = Workbook() (excel_file) for sheet_index in range(): sheet = [sheet_index] for i, shape in enumerate(): image = () image_path = f"{output_directory}/{}_Geometric shapes_{i}.png" (image_path) () # Usage examplesave_geometric_shapes_as_images("Example.xlsx", "Output Directory")
The following code shows how to save text boxes in an Excel file as an image:
from import * def save_textboxes_as_images(excel_file, output_directory): """ Will specify Excel All text boxes in the file are exported as pictures,and save to the specified output directory。 parameter: excel_file (str): Contains text box Excel File path。 output_directory (str): The destination directory path for image saving。 """ workbook = Workbook() (excel_file) for sheet_index in range(): sheet = [sheet_index] for i, shape in enumerate(): image = () image_path = f"{output_directory}/{}_Text box_{i}.png" (image_path) () # Usage examplesave_textboxes_as_images("Example.xlsx", "Output Directory")
This is the article about Python implementing exporting chart shapes and other contents in Excel into pictures. For more related Python Excel content to export content as picture content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!