I searched for solutions on the Internet and felt that converting Excel tables into HTML and then using platwright screenshots is a relatively smooth path because there is a smooth tool chain. If you are using a Windows system, you do not need to read this article because the win32com library is more convenient. The Excel-to-HTML scheme in this article mainly makes up for the presence of merged cells in other solutions online. The code is used to help generate Zhipu Qingyan, and some variable controls still need to be changed by yourself.
The specific implementation code is as follows
from openpyxl import load_workbook from import Font, Border, Side, Alignment from playwright.sync_api import sync_playwright from datetime import datetime # Open the browser and take a screenshotdef capture_table_screenshot( url, output_file, table_selector): with sync_playwright() as p: browser = (headless=False) page = browser.new_page() # Note that you need to add an agreement here ("file://" + url) # Wait for the table element to load page.wait_for_selector(table_selector) page.wait_for_timeout(1000) # Take screenshots of table elements table_element = (table_selector) table_element.screenshot(path=output_file) () # The text content of the merged cell is placed in the upper left cell by default. If not, it needs to be processed by a special program.# The border style defaults to 1px soliddef read_excel(file_path): # data_only calculates the formula in the Excel table into numerical values and reads it out. wb = load_workbook( filename=file_path, data_only=True) ws = # Read the activity worksheet data = [] merges = [] # Used to store information about merged cells cell_styles = [] # Read merged cell information for merged_range in ws.merged_cells.ranges: start_row, start_col = merged_range.min_row, merged_range.min_col end_row, end_col = merged_range.max_row, merged_range.max_col ((start_row-1, start_col-1, end_row-1, end_col-1)) for row in ws.iter_rows(): row_data = [] row_styles = [] for cell in row: print(f"The coordinates of the current cell:{}") if in ws.merged_cells.ranges: # Skip non-start cells in merged cells continue if is not None: print(f"The value of the cell:{}") row_data.append(str()) else: row_data.append('') # Fill empty strings with empty cells # Read cell style and provide default values font = if else Font() border = if else Border() alignment = if else Alignment() print(f"Cell font color:{}") print(f"Cell border style:{}") cell_style = { 'font': { 'name': if else 'Arial', 'size': if else 12, 'bold': if else False, 'italic': if else False, 'color': if and else '#000000' }, 'border': { 'top': '1px solid' if and else None, 'left': '1px solid' if and else None, 'right': '1px solid' if and else None, 'bottom': '1px solid' if and else None }, 'alignment': { 'horizontal': if else None, 'vertical': if else None } } row_styles.append(cell_style) print(f"Converted cell style:{cell_style}") (row_data) cell_styles.append(row_styles) return data, merges, cell_styles # By default, there is only the same row and multiple columns merged. If the merged cells occupy two rows, additional processing is required.def generate_html_table(data, merges, cell_styles): print(f"Merge cell information:{merges}") html = "<table style='border-collapse: collapse;'>\n" for row_idx, row in enumerate(data): print("-"*20) print(f"Data of the current row:{row}") html += "<tr>\n" # Set a tag that skips non-first merged cells skip_next_cell = 0 for col_idx,cell in enumerate(row): if skip_next_cell > 0: skip_next_cell -= 1 continue # Line number and column number start from 0 print(f"当前The value of the cell:{cell},Line number:{row_idx},Column number:{col_idx}") # If the current cell is 1 row and 4 columns, modify the cell value if row_idx == 1 and col_idx == 4: # Get today's date today = () cell = formatted_date_no_leading_zeros = "Deadline" + ("%-m month %-d day") print(f"Modified cell value:{cell}") # Remove cell styles style = cell_styles[row_idx][col_idx] if style: font_style = f"font-family:{style['font']['name']}; font-size:{style['font']['size']}pt; " \ f"font-weight:{'bold' if style['font']['bold'] else 'normal'}; " \ f"font-style:{'italic' if style['font']['italic'] else 'normal'};" border_style = f"border-top:{style['border']['top']}; " \ f"border-left:{style['border']['left']}; " \ f"border-right:{style['border']['right']}; " \ f"border-bottom:{style['border']['bottom']};" alignment_style = f"text-align:{style['alignment']['horizontal']}; " \ f"vertical-align:{style['alignment']['vertical']};" if (row_idx, col_idx) in [(m[0], m[1]) for m in merges]: # Check whether the current cell is the starting cell of the merged cell rowspan = [m[2] - m[0] + 1 for m in merges if m[0] == row_idx and m[1] == col_idx][0] colspan = [m[3] - m[1] + 1 for m in merges if m[0] == row_idx and m[1] == col_idx][0] if style: html += f"<td style='{font_style} {border_style} {alignment_style}' rowspan={rowspan} colspan={colspan}>{cell}</td>" else: html += f"<td rowspan={rowspan} colspan={colspan}>{cell}</td>" skip_next_cell = colspan - 1 # Skip the merged columns else: if style: html += f"<td style='{font_style} {border_style} {alignment_style}' >{cell}</td>" else: html += f"<td>{cell}</td>" html += "</tr>\n" html += "</table>" html = "<!DOCTYPE html><html><head><meta charset='UTF-8'><title>Excel Table</title></head><body>" + html + "</body></html>" return html def main(): current_dir = 'reer' excel_file_path = current_dir + 'log/' # Replace with your Excel file path html_file_path = current_dir + 'log/' screenshot_file_path = current_dir + 'log/table_screenshot.png' data, merges, cell_styles = read_excel(excel_file_path) html_table = generate_html_table(data, merges, cell_styles) with open(html_file_path, 'w', encoding='utf-8') as file: (html_table) # Call the function and replace the following parameters url = html_file_path # Web URL output_file = screenshot_file_path # Output file path table_selector = 'table' # The CSS selector of the table, adjust it according to the actual situation capture_table_screenshot(url, output_file, table_selector) if __name__ == "__main__": main()
This is the end of this article about how to use Python to implement screenshots for Excel tables. For more related content on Python Excel screenshots, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!