暂时不删除临时文件
This commit is contained in:
@@ -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__":
|
||||
|
||||
Reference in New Issue
Block a user