fix
This commit is contained in:
@@ -109,12 +109,12 @@ def _build_output_url(abs_output_path: str) -> Optional[str]:
|
||||
|
||||
|
||||
@mcp.tool()
|
||||
async def list_docx_images(docx_path: str) -> List[Dict[str, Any]]:
|
||||
async def list_docx_images(docx_url: str) -> List[Dict[str, Any]]:
|
||||
"""
|
||||
列出指定 DOCX 文件中的所有图片信息。
|
||||
|
||||
参数:
|
||||
- docx_path: DOCX 文件的路径(相对或绝对),也可以是 HTTP/HTTPS URL。
|
||||
- docx_url: 文件的HTTP/HTTPS URL。
|
||||
|
||||
返回:
|
||||
- 图片信息列表,每一项包含:
|
||||
@@ -124,38 +124,11 @@ async def list_docx_images(docx_path: str) -> List[Dict[str, Any]]:
|
||||
- docpr_name: Word 内部的图片名称
|
||||
- width_cm / height_cm: 近似尺寸(厘米),可能为 None
|
||||
"""
|
||||
tmp_file: Optional[str] = None
|
||||
try:
|
||||
local_path = docx_path
|
||||
if _is_url(docx_path):
|
||||
parsed = urllib.parse.urlparse(docx_path)
|
||||
ext = os.path.splitext(parsed.path)[1] or ".docx"
|
||||
tmp_file = _download_to_temp(docx_path, suffix=ext)
|
||||
local_path = tmp_file
|
||||
|
||||
if not os.path.exists(local_path):
|
||||
raise FileNotFoundError(f"DOCX 文件不存在: {docx_path}")
|
||||
|
||||
imgs = get_images_info(local_path)
|
||||
# 为了避免泄露容器内部路径,屏蔽 abs_path 字段
|
||||
for img in imgs:
|
||||
img.pop("abs_path", None)
|
||||
return imgs
|
||||
finally:
|
||||
if tmp_file and os.path.exists(tmp_file):
|
||||
try:
|
||||
os.remove(tmp_file)
|
||||
except OSError:
|
||||
pass
|
||||
|
||||
imgs = get_images_info(_download_to_temp(docx_url, suffix=".docx"))
|
||||
return imgs
|
||||
|
||||
@mcp.tool()
|
||||
async def edit_docx(
|
||||
input_docx: str,
|
||||
output_docx: str,
|
||||
replacements: Optional[List[Dict[str, str]]] = None,
|
||||
image_replacements: Optional[List[Dict[str, Any]]] = None,
|
||||
) -> Dict[str, Any]:
|
||||
async def edit_docx(input_docx_url: str, output_docx_url: str, replacements: Optional[List[Dict[str, str]]] = None, image_replacements: Optional[List[Dict[str, Any]]] = None) -> Dict[str, Any]:
|
||||
"""
|
||||
使用原始 mcp_docx 逻辑对 DOCX 文件进行编辑。
|
||||
|
||||
@@ -165,8 +138,8 @@ async def edit_docx(
|
||||
- 替换指定序号的图片
|
||||
|
||||
参数:
|
||||
- input_docx: 输入 DOCX 文件路径,或 HTTP/HTTPS URL
|
||||
- output_docx: 输出 DOCX 文件路径
|
||||
- input_docx_url: 输入 DOCX 文件路径,或 HTTP/HTTPS URL
|
||||
- output_docx_url: 输出 DOCX 文件路径
|
||||
- replacements: 文本替换规则列表,例如:
|
||||
[
|
||||
{\"old\": \"旧标题\", \"new\": \"<span color='#FF0000'>新标题</span>\"},
|
||||
@@ -189,15 +162,15 @@ async def edit_docx(
|
||||
tmp_images: List[str] = []
|
||||
|
||||
try:
|
||||
local_input = input_docx
|
||||
if _is_url(input_docx):
|
||||
parsed = urllib.parse.urlparse(input_docx)
|
||||
local_input = input_docx_url
|
||||
if _is_url(input_docx_url):
|
||||
parsed = urllib.parse.urlparse(input_docx_url)
|
||||
ext = os.path.splitext(parsed.path)[1] or ".docx"
|
||||
tmp_input = _download_to_temp(input_docx, suffix=ext)
|
||||
tmp_input = _download_to_temp(input_docx_url, suffix=ext)
|
||||
local_input = tmp_input
|
||||
|
||||
if not os.path.exists(local_input):
|
||||
raise FileNotFoundError(f"输入 DOCX 文件不存在: {input_docx}")
|
||||
raise FileNotFoundError(f"输入 DOCX 文件不存在: {input_docx_url}")
|
||||
|
||||
if replacements is None:
|
||||
replacements = []
|
||||
@@ -247,13 +220,13 @@ async def edit_docx(
|
||||
# 复用原始处理函数
|
||||
process(
|
||||
input_docx=local_input,
|
||||
output_docx=output_docx,
|
||||
output_docx=output_docx_url,
|
||||
replacements=rep_pairs,
|
||||
image_replacements=img_pairs,
|
||||
color_keywords=color_keywords,
|
||||
)
|
||||
|
||||
abs_out = os.path.abspath(output_docx)
|
||||
abs_out = os.path.abspath(output_docx_url)
|
||||
return {
|
||||
"output_path": abs_out,
|
||||
"output_url": _build_output_url(abs_out),
|
||||
|
||||
Reference in New Issue
Block a user