如何利用Python实现给Excel表格截图

2025-02-08 16:50

本文主要是介绍如何利用Python实现给Excel表格截图,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

《如何利用Python实现给Excel表格截图》这篇文章主要为大家详细介绍了如何利用Python实现给Excel表格截图功能,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下...

我搜索了网络上的方案,感觉把 Excel 表格转换为 html 再用 platwright 截图是比China编程较顺畅的路径,因为有顺畅的工具链。如果使用的是 Windows 系统则不需要阅读此文,因为 win32com 库更方便。这篇文章中 Excel 转 HTML 的方案,主要弥补了网上其他方案中存在合并单元格的情况。代码为智谱清言帮助生成,有些变量控制还是需要自己改一下。

具体实现代码如下

 
from openpyxl import load_workbook
from openpyxl.styles import Font, Border, Side, Alignment
from playwright.sync_api import sync_playwright
from datetime import datetime
 
# 打开浏览器并截图
def capturjavascripte_table_screenshot( url, output_file, table_selector):
    with sync_playwright() as p:
        browser = p.chromium.launch(headless=False)
        page = browser.new_page()
        # 注意这里需要加协议
        page.goto("file://" + url)
        
        # 等待表格元素加载完成
        page.wait_for_selector(table_selector)
        page.wait_for_timeout(1000)
        
        # 对表格元素进行截图
        table_element = page.locator(table_selector)
        table_element.screenshot(path=output_file)
        
        browser.close()
 
# 默认合并单元格的文本内容是放在左上单元格的,如果不是,需要专门程序处理。
# 边框样式默认为1px solid
def read_excel(file_path):
    # data_only 将 Excel 表格里的公式计算成数值读取出来。
    wb = load_workbook( filename=file_path, data_only=True)
    ws = wb.active  # 读取活动工作表
    data = []
    merges = []  # 用于存储合并单元格的信息
    cell_styles = []
    
    # 读取合并单元格信息
    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
        merges.append((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"当前单元格的坐标:{cell.coordinate}")
            if cell.coordinate in ws.merged_cells.ranges:
                # 跳过合并单元格中的非起始单元格
                continue            
            if cell.value is not None:
                print(f"单元格的值:{cell.value}")
                row_data.append(str(cell.value))                
            else:
                row_data.append('')  # 空单元格填充空字符串
            # 读取单元格样式,提供默认值
            font = cell.font if cell.font else Font()
            border = cell.border if cell.border else Border()
            alignment = cell.alignment if cell.alignment else Alignment()
 
            print(f"单元格字体颜色:{font.color.index}")
            print(f"单元格边框样式:{border.top.style}")
            cell_style = {
                'font': {
                    'name': font.name if font.name else 'Arial',
                    'size': font.size if font.size else 12,
                    'bold': font.bold if font.bold else False,
                    'italic': font.italic if font.italic else False,
                    'color': font.color.rgb if font.color and font.color.rgb else '#000000'
                },
                'border': {
                    'top': '1px solid' if border.top and border.top.style else Nonpythone,
                    'left': '1px solid' if border.left and border.left.style else None,
                    'right': '1px sowww.chinasem.cnlid' if border.right and border.right.style else None,
                    'bottom': '1px solid' if border.bottom and border.bottom.style else None
                },
                'alignment': {
                    'horizontal': alignment.horizontal if alignment.horizontal else None,
                    'vertical': alignment.vertical if alignment.vertical else None
                }
            }
            row_styles.append(cell_style)
            print(f"转换后的单元格样式:{cell_style}")
 
        data.append(row_data)
        cell_styles.append(row_styles)      
 
    return data, merges, cell_styles
 
# 该处默认只有同一行合并多列的情况。如果合并单元格占了两行,需要另外的处理。
def generate_html_table(data, merges, cell_styles):
    print(f"合并单元格的信息:{merges}")
    html = "<table style='border-collapse: collapse;'>\n"
    for row_idx, row in enumerate(data):
        print("-"*20)
        print(f"当前行的数据:{row}")
        html += "<tr>\n"
        # 设置一个跳过非首个合并单元格的标记
        skip_next_cell = 0
        for col_idx,cell in enumerate(row):
            if skip_next_cell > 0:
                skip_next_cell -= 1
                continue
            # 行号、列号从0开始
            print(f"当前单元格的值:{cell},行号:{row_idx},列号:{col_idx}")
            # 如果当前单元格为1行4列,则修改cell值
            if row_idx == 1 and col_idx == 4:
                # 获取今天的日期
                today = datetime.today()
                cell = formatted_date_no_leading_zeros = "截止 " + today.strftime("%-m 月 %-d 日")
                print(f"修改后的单元格值:{cell}")
            # 去除单元格样式
            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]:  # 检查当前单元格是否是合并单元格的起始单元格
                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"&lChina编程t;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    # 跳过合并的列
            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/2re0207.xlsx'  # 替换为你的Excel文件路径
    html_file_path = current_dir + 'log/output.html'
    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:
        file.write(html_table)
    # 调用函数,替换以下参数
    url = html_file_path  # 网页URL
    output_file = screenshot_file_path  # 输出文件路径
    table_selector = 'table'  # 表格的css选择器,根据实际情况调整
    capture_table_screenshot(url, output_file, table_selector)
        
if __name__ == "__main__":
    main()

到此这篇关于如何利用python实现给Excel表格截图的文章就介绍到这了,更多相关Python Excel截图内容请搜索编程China编程(www.chinasem.cn)以前的文章或继续浏览下面的相关文章希望大家以后多多支持China编程(www.chinasem.cn)!

这篇关于如何利用Python实现给Excel表格截图的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1153349

相关文章

Python实现终端清屏的几种方式详解

《Python实现终端清屏的几种方式详解》在使用Python进行终端交互式编程时,我们经常需要清空当前终端屏幕的内容,本文为大家整理了几种常见的实现方法,有需要的小伙伴可以参考下... 目录方法一:使用 `os` 模块调用系统命令方法二:使用 `subprocess` 模块执行命令方法三:打印多个换行符模拟

SpringBoot+EasyPOI轻松实现Excel和Word导出PDF

《SpringBoot+EasyPOI轻松实现Excel和Word导出PDF》在企业级开发中,将Excel和Word文档导出为PDF是常见需求,本文将结合​​EasyPOI和​​Aspose系列工具实... 目录一、环境准备与依赖配置1.1 方案选型1.2 依赖配置(商业库方案)二、Excel 导出 PDF

Python实现MQTT通信的示例代码

《Python实现MQTT通信的示例代码》本文主要介绍了Python实现MQTT通信的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一... 目录1. 安装paho-mqtt库‌2. 搭建MQTT代理服务器(Broker)‌‌3. pytho

基于Python开发一个图像水印批量添加工具

《基于Python开发一个图像水印批量添加工具》在当今数字化内容爆炸式增长的时代,图像版权保护已成为创作者和企业的核心需求,本方案将详细介绍一个基于PythonPIL库的工业级图像水印解决方案,有需要... 目录一、系统架构设计1.1 整体处理流程1.2 类结构设计(扩展版本)二、核心算法深入解析2.1 自

使用zip4j实现Java中的ZIP文件加密压缩的操作方法

《使用zip4j实现Java中的ZIP文件加密压缩的操作方法》本文介绍如何通过Maven集成zip4j1.3.2库创建带密码保护的ZIP文件,涵盖依赖配置、代码示例及加密原理,确保数据安全性,感兴趣的... 目录1. zip4j库介绍和版本1.1 zip4j库概述1.2 zip4j的版本演变1.3 zip4

从入门到进阶讲解Python自动化Playwright实战指南

《从入门到进阶讲解Python自动化Playwright实战指南》Playwright是针对Python语言的纯自动化工具,它可以通过单个API自动执行Chromium,Firefox和WebKit... 目录Playwright 简介核心优势安装步骤观点与案例结合Playwright 核心功能从零开始学习

Python 字典 (Dictionary)使用详解

《Python字典(Dictionary)使用详解》字典是python中最重要,最常用的数据结构之一,它提供了高效的键值对存储和查找能力,:本文主要介绍Python字典(Dictionary)... 目录字典1.基本特性2.创建字典3.访问元素4.修改字典5.删除元素6.字典遍历7.字典的高级特性默认字典

Python自动化批量重命名与整理文件系统

《Python自动化批量重命名与整理文件系统》这篇文章主要为大家详细介绍了如何使用Python实现一个强大的文件批量重命名与整理工具,帮助开发者自动化这一繁琐过程,有需要的小伙伴可以了解下... 目录简介环境准备项目功能概述代码详细解析1. 导入必要的库2. 配置参数设置3. 创建日志系统4. 安全文件名处

使用Python构建一个高效的日志处理系统

《使用Python构建一个高效的日志处理系统》这篇文章主要为大家详细讲解了如何使用Python开发一个专业的日志分析工具,能够自动化处理、分析和可视化各类日志文件,大幅提升运维效率,需要的可以了解下... 目录环境准备工具功能概述完整代码实现代码深度解析1. 类设计与初始化2. 日志解析核心逻辑3. 文件处

python生成随机唯一id的几种实现方法

《python生成随机唯一id的几种实现方法》在Python中生成随机唯一ID有多种方法,根据不同的需求场景可以选择最适合的方案,文中通过示例代码介绍的非常详细,需要的朋友们下面随着小编来一起学习学习... 目录方法 1:使用 UUID 模块(推荐)方法 2:使用 Secrets 模块(安全敏感场景)方法