From 4db99a882a184084ae0173eb99afca7091072357 Mon Sep 17 00:00:00 2001 From: liuwei Date: Fri, 16 May 2025 09:12:38 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E6=B5=8F=E8=A7=88=E5=99=A8?= =?UTF-8?q?=E7=AD=96=E7=95=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- utils/markdown_to_image.py | 75 ++++++++++++++++++++++++++------------ 1 file changed, 51 insertions(+), 24 deletions(-) diff --git a/utils/markdown_to_image.py b/utils/markdown_to_image.py index b8284ef..2e4b375 100644 --- a/utils/markdown_to_image.py +++ b/utils/markdown_to_image.py @@ -160,30 +160,57 @@ async def html_to_image(html_file, output_image): """ 使用 Playwright 加载 HTML 文件并截图(异步)。 """ - async with async_playwright() as p: - possible_chrome_paths = [ - r"C:\Users\Liu_WIN10\AppData\Local\Google\Chrome\Application\chrome.exe", - r"C:\Users\Liu-OPEN\AppData\Local\Google\Chrome\Application\chrome.exe", - r"C:\Program Files\Google\Chrome\Application\chrome.exe", - r"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" - ] - browser_path = None - for path in possible_chrome_paths: - if os.path.exists(path): - browser_path = path - print(f"找到浏览器路径: {browser_path}") - break - if browser_path: - browser = await p.chromium.launch(executable_path=browser_path) - else: - print("未找到指定的Chrome浏览器路径,使用默认浏览器") - browser = await p.chromium.launch() - page = await browser.new_page() - await page.goto(f'file://{os.path.abspath(html_file)}') - await page.set_viewport_size({"width": 750, "height": 800}) - await page.wait_for_timeout(500) - await page.screenshot(path=output_image, full_page=True) - await browser.close() + try: + async with async_playwright() as p: + # 检查系统类型 + if os.name == 'nt': # Windows + possible_chrome_paths = [ + r"C:\Users\Liu_WIN10\AppData\Local\Google\Chrome\Application\chrome.exe", + r"C:\Users\Liu-OPEN\AppData\Local\Google\Chrome\Application\chrome.exe", + r"C:\Program Files\Google\Chrome\Application\chrome.exe", + r"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" + ] + browser_path = None + for path in possible_chrome_paths: + if os.path.exists(path): + browser_path = path + print(f"找到浏览器路径: {browser_path}") + break + if browser_path: + browser = await p.chromium.launch(executable_path=browser_path) + else: + print("未找到指定的Chrome浏览器路径,尝试使用默认浏览器") + # 尝试安装 Playwright 浏览器 + try: + import subprocess + print("正在安装 Playwright 浏览器...") + subprocess.run(["playwright", "install"], check=True) + print("Playwright 浏览器安装完成") + except Exception as install_error: + print(f"安装 Playwright 浏览器失败: {install_error}") + browser = await p.chromium.launch() + else: # Linux + try: + # 尝试安装 Playwright 浏览器 + import subprocess + print("正在安装 Playwright 浏览器...") + subprocess.run(["playwright", "install"], check=True) + print("Playwright 浏览器安装完成") + except Exception as install_error: + print(f"安装 Playwright 浏览器失败: {install_error}") + browser = await p.chromium.launch() + + page = await browser.new_page() + await page.goto(f'file://{os.path.abspath(html_file)}') + await page.set_viewport_size({"width": 750, "height": 800}) + await page.wait_for_timeout(500) + await page.screenshot(path=output_image, full_page=True) + await browser.close() + except Exception as e: + print(f"浏览器操作失败: {e}") + if "Executable doesn't exist" in str(e): + print("请运行 'playwright install' 命令安装必要的浏览器组件") + raise # 主函数:从字符串转换 Markdown 到图片(异步版)