You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.
import subprocess
import os
import logging
# 获取日志记录器
logger = logging . getLogger ( __name__ )
def doc2docx ( input_path , output_dir = None ) :
logger . info ( " 开始将doc转成docx " )
# 如果未指定输出目录,则使用输入文件所在目录
if output_dir is None :
output_dir = os . path . dirname ( input_path )
# 构建输出文件路径
output_file = os . path . join (
output_dir , os . path . splitext ( os . path . basename ( input_path ) ) [ 0 ] + " .docx "
)
# LibreOffice 命令行工具
libreoffice_cmd = " soffice "
# 构建转换命令
command = [
libreoffice_cmd ,
" --headless " , # 无界面模式
" --convert-to " ,
" docx " , # 转换为 docx
" --outdir " ,
output_dir , # 输出目录
input_path , # 输入文件
]
# 执行命令
try :
subprocess . run ( command , check = True )
print ( f " 转换成功: { output_file } " )
return output_file
except subprocess . CalledProcessError as e :
print ( f " 转换失败: { e } " )
return None
if __name__ == " __main__ " :
input_path = " ./南方电网公司“抢修、投诉、舆情”三工单联动监测日报( 3月6日) .doc " # 替换为你的 .doc 文件路径
doc2docx ( input_path )