From e388deb4ea892f9e793d2ba5f0e021712acdd3d2 Mon Sep 17 00:00:00 2001 From: liuwei Date: Mon, 16 Jun 2025 17:18:37 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9A=82=E6=97=B6=E4=B8=8D=E5=88=A0=E9=99=A4?= =?UTF-8?q?=E4=B8=B4=E6=97=B6=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- utils/markdown_to_image.py | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/utils/markdown_to_image.py b/utils/markdown_to_image.py index 430c0f7..a554570 100644 --- a/utils/markdown_to_image.py +++ b/utils/markdown_to_image.py @@ -187,7 +187,7 @@ async def html_to_image(html_file, output_image): for path in possible_chrome_paths: if check_chromium_installed(path): browser_path = path - print(f"找到浏览器路径: {browser_path}") + logger.debug(f"找到浏览器路径: {browser_path}") break else: # Linux import glob @@ -199,17 +199,17 @@ async def html_to_image(html_file, output_image): for path in sorted(chrome_paths, reverse=True): # 按版本名排序,最新优先 if check_chromium_installed(path): browser_path = path - print(f"找到 Playwright Chromium 路径: {browser_path}") + logger.debug(f"找到 Playwright Chromium 路径: {browser_path}") break if not browser_path: - print("未找到已安装的 Chromium 浏览器,尝试使用 Playwright 默认安装") + logger.debug("未找到已安装的 Chromium 浏览器,尝试使用 Playwright 默认安装") try: - print("正在安装 Playwright 浏览器...") + logger.debug("正在安装 Playwright 浏览器...") subprocess.run(["playwright", "install", "chromium"], check=True) - print("Playwright 浏览器安装完成") + logger.debug("Playwright 浏览器安装完成") except Exception as install_error: - print(f"安装 Playwright 浏览器失败: {install_error}") + logger.debug(f"安装 Playwright 浏览器失败: {install_error}") browser = await p.chromium.launch() # 使用默认路径 else: @@ -223,15 +223,15 @@ async def html_to_image(html_file, output_image): await page.wait_for_timeout(500) await page.screenshot(path=output_image, full_page=True) except Exception as e: - print(f"截图失败: {e}") + logger.exception(f"截图失败: {e}") finally: await page.close() await browser.close() except Exception as e: - print(f"浏览器操作失败: {e}") + logger.debug(f"浏览器操作失败: {e}") if "Executable doesn't exist" in str(e): - print("请运行 'playwright install' 命令安装必要的浏览器组件") + logger.debug("请运行 'playwright install' 命令安装必要的浏览器组件") raise @@ -289,15 +289,15 @@ async def convert_md_str_to_image(md_content: str, output_image: str) -> str: except Exception as e: logger.error(f"Error converting markdown to image: {e}") raise - finally: - # 可选:清理临时 HTML 文件 - if temp_html_path.exists(): - try: - # 使用异步方式删除文件 - await asyncio.to_thread(os.remove, str(temp_html_path)) - logger.debug(f"Deleted temporary HTML file: {temp_html_path}") - except Exception as e: - logger.warning(f"Failed to delete temporary HTML file: {e}") + # finally: + # # 可选:清理临时 HTML 文件 + # if temp_html_path.exists(): + # try: + # # 使用异步方式删除文件 + # # await asyncio.to_thread(os.remove, str(temp_html_path)) + # # logger.debug(f"Deleted temporary HTML file: {temp_html_path}") + # except Exception as e: + # logger.warning(f"Failed to delete temporary HTML file: {e}") # 示例使用 if __name__ == "__main__":