background
In daily office, I often need to select a range cell in batches of Excel and then convert it to image format
1. Introduction to the library
openpyxl
Openpyxl is a powerful Python library that is mainly used to read, write and manipulate Excel files (especially in the .xlsx format). It provides a set of feature-rich APIs that support Excel 2010 and later file formats, making it very easy to process Excel files in programming. The main features and functions of Openpyxl include:
1. Support .xlsx format: Openpyxl is mainly used to process .xlsx files in Excel 2010 and later versions.
2. Read and write Excel files: Users can use Openpyxl to read existing Excel files, obtain data, modify data, and save them to a new file.
3. Operate cells: Openpyxl allows users to read and write data according to rows, columns or specific cells.
4. Create and modify worksheets: Users can create new worksheets, copy and delete existing worksheets, and set worksheet properties, etc.
5. Style settings: Openpyxl allows users to set the font, color, border and other styles of cells.
6. Charts and formulas: Users can create charts, add formulas, etc. through Openpyxl.
7. Supports numeric and date formats: Openpyxl can correctly handle numeric and date formats to ensure that the correct format is displayed in Excel.
In Openpyxl, there are three main objects: Workbook, Worksheet, and Cell. Workbook represents an Excel document, Worksheet represents a table, and Cell represents a cell. These objects contain many attributes and methods, which facilitates users to perform various operations.
Installing and importing Openpyxl is also relatively simple. Users can use pip for installation and then import the entire library or specific modules and functions through import statements in a Python program.
Overall, Openpyxl is a powerful and easy-to-use Python library that can greatly improve the efficiency of processing Excel files. Whether it’s automating large amounts of data or creating beautiful reports, Openpyxl is a powerful tool.
2. Installation of the library
Library | use | Install |
---|---|---|
openpyxl | Reading and writing of Excel | pip install openpyxl -i /simple/ |
matplotlib | Image generation | pip install matplotlib -i /simple/ |
os | Get the absolute path | Built-in library does not require installation |
3. Core code
Extract data
# parse cell rangestart_col = .column_index_from_string(start_cell[0]) start_row = int(start_cell[1:]) end_col = .column_index_from_string(end_cell[0]) end_row = int(end_cell[1:]) # Extract datadata = [[(row=i, column=j).value or "" for j in range(start_col, end_col + 1)] for i in range(start_row, end_row + 1)] # Create a graphfig, ax = () ('tight') ('off') table = (cellText=data, loc='center', cellLoc='center')
screenshot
# Save the picturefile_name = ((file_path))[0] output_path = (output_folder, f"{file_name}.png") (output_path, bbox_inches='tight', dpi=300)
4. Regular version
import openpyxl import as plt from matplotlib import rcParams # Set the font to Microsoft YaheircParams[''] = 'Microsoft YaHei' # Read Excel filewb = openpyxl.load_workbook(r"C:\Users\Xiaozhuang's Y9000P\Desktop\Data") sheet = # User input cell rangestart_cell = "A1" end_cell = "C3" # parse the rows and columns of the start and end cellsstart_col = .column_index_from_string(start_cell[0]) start_row = int(start_cell[1:]) end_col = .column_index_from_string(end_cell[0]) end_row = int(end_cell[1:]) # Extract cell contentdata = [[(row=i, column=j).value for j in range(start_col, end_col + 1)] for i in range(start_row, end_row + 1)] # Create graphics and axesfig, ax = () # Hide axes('tight') ('off') # Create a tabletable = (cellText=data, loc='center', cellLoc='center') # Set font sizefor (i, j), cell in table.get_celld().items(): cell.set_fontsize(12) # Set the font size to 12 cell.set_edgecolor('black') # Set border color # Save as picture('', bbox_inches='tight', dpi=300) ()
This is the article about Python implementing screenshots of Excel cell content. For more related contents for screenshots of Python Excel cell, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!