Files
gangyan/langchain-chat/server/translator_service/converter/doc.py

56 lines
1.6 KiB
Python

import subprocess
import os
import asyncio
from configs.basic_config import *
async def convert_doc_to_docx(file_path: str) -> bool:
"""使用 libreoffice 将 doc 文件转换为 docx 文件, 替换原来的文件"""
try:
# 检查文件是否存在
if not os.path.exists(file_path):
raise FileNotFoundError(f"文件不存在: {file_path}")
# 获取文件所在目录和文件名
file_dir = os.path.dirname(file_path)
file_name = os.path.basename(file_path)
# 构建 libreoffice 命令
cmd = [
"soffice",
"--headless",
"--convert-to",
"docx",
"--outdir",
file_dir,
file_path,
]
# 执行转换
process = await asyncio.create_subprocess_exec(
*cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE
)
stdout, stderr = await process.communicate()
if process.returncode != 0:
raise Exception(f"转换失败: {stderr.decode()}")
# 获取转换后的文件路径
docx_file = os.path.join(file_dir, os.path.splitext(file_name)[0] + ".docx")
# 检查转换后的文件是否存在
if not os.path.exists(docx_file):
raise FileNotFoundError(f"转换后的文件不存在: {docx_file}")
# 删除原文件并重命名新文件
os.remove(file_path)
os.rename(docx_file, file_path)
logger.info(f"成功将 {file_path} 转换为 docx 格式")
return True
except Exception as e:
logger.error(f"转换 doc 到 docx 失败: {str(e)}")
raise