调整浏览器路径。

This commit is contained in:
liuwei
2025-05-26 14:17:36 +08:00
parent 1018d09a10
commit 211624eec9

View File

@@ -1,12 +1,11 @@
import subprocess
import markdown
from playwright.async_api import async_playwright
import os
import asyncio
def playwright_browser_installed():
"""检查 Playwright 的 Chromium 浏览器是否已安装"""
chromium_path = os.path.expanduser("~/.cache/ms-playwright/chromium")
return os.path.exists(chromium_path)
# linux 下需要安装字体
# sudo apt-get install -y fonts-noto-cjk fonts-noto-cjk-extra
# sudo apt-get install -y fonts-noto-color-emoji fonts-noto-cjk fonts-wqy-microhei
@@ -161,47 +160,64 @@ def md_str_to_html(md_content, output_html):
f.write('</body></html>')
# 使用 Playwright 将 HTML 渲染并截图(异步版)
def check_chromium_installed(path):
return os.path.isfile(path) and os.access(path, os.X_OK)
async def html_to_image(html_file, output_image):
"""
使用 Playwright 加载 HTML 文件并截图(异步)。
"""
try:
async with async_playwright() as p:
# Windows 系统
if os.name == 'nt':
browser_path = None
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 = next((p for p in possible_chrome_paths if os.path.exists(p)), None)
if browser_path:
print(f"找到浏览器路径: {browser_path}")
browser = await p.chromium.launch(executable_path=browser_path)
else:
print("未找到 Chrome尝试使用 Playwright 自带浏览器")
if not playwright_browser_installed():
print("Playwright 浏览器未安装,正在安装...")
import subprocess
subprocess.run(["playwright", "install", "chromium"], check=True)
print("安装完成")
browser = await p.chromium.launch()
else:
if not playwright_browser_installed():
print("Playwright 浏览器未安装,正在安装...")
import subprocess
subprocess.run(["playwright", "install", "chromium"], check=True)
print("安装完成")
browser = await p.chromium.launch()
for path in possible_chrome_paths:
if check_chromium_installed(path):
browser_path = path
print(f"找到浏览器路径: {browser_path}")
break
else: # Linux
import glob
user_home = os.path.expanduser("~")
glob_pattern = os.path.join(user_home, ".cache", "ms-playwright", "chromium-*", "chrome-linux",
"chrome")
chrome_paths = glob.glob(glob_pattern)
browser_path = None
for path in sorted(chrome_paths, reverse=True): # 按版本名排序,最新优先
if check_chromium_installed(path):
browser_path = path
print(f"找到 Playwright Chromium 路径: {browser_path}")
break
if not browser_path:
print("未找到已安装的 Chromium 浏览器,尝试使用 Playwright 默认安装")
try:
print("正在安装 Playwright 浏览器...")
subprocess.run(["playwright", "install", "chromium"], check=True)
print("Playwright 浏览器安装完成")
except Exception as install_error:
print(f"安装 Playwright 浏览器失败: {install_error}")
browser = await p.chromium.launch() # 使用默认路径
else:
browser = await p.chromium.launch(executable_path=browser_path)
# 业务逻辑不变
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):
@@ -221,6 +237,7 @@ async def convert_md_str_to_image(md_content, output_image):
print(f"图片已生成:{output_image}")
return os.path.abspath(output_image)
# 示例使用
if __name__ == "__main__":
# 示例 Markdown 字符串(包含中文和 Emoji