You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

73 lines
2.8 KiB

2 weeks ago
import os
import subprocess
import uuid
# 获取MathType对应的Latex公式
def get_latex_list(docx_file):
# 获取当前目录的父级目录
current_dir = os.path.dirname(os.path.abspath(__file__))
current_dir = os.path.dirname(current_dir)
mtef = os.path.join(current_dir, 'mtef-go-3', 'mtef-go.exe')
res = []
output = os.path.join(os.environ['TEMP'], uuid.uuid4().hex + '.txt')
command = mtef + r" -w " + docx_file + " -o " + output
os.system(command)
# 如果output没有正确生成
if not os.path.exists(output):
return res
with open(output, 'r', encoding='utf-8') as file:
for i, line in enumerate(file):
2 weeks ago
res.append(line.strip().replace("$$", ""))
2 weeks ago
return res
# 结合Pandoc和mtef-go的结果合并成最终的输出文本
def get_docx_content_by_pandoc(docx_file):
# 一、获取Latex公式列表
formula_list = get_latex_list(docx_file)
# 最后拼接的内容
content = ""
# output_file 设置为临时目录下的uuid.md
2 weeks ago
file_name = uuid.uuid4().hex
temp_markdown = os.path.join('../static/markdown/', file_name + '.md')
2 weeks ago
# 调用pandoc将docx文件转换成markdown
2 weeks ago
os.mkdir("../static/Images/" + file_name)
subprocess.run(['pandoc', docx_file, '-f', 'docx', '-t', 'markdown', '-o', temp_markdown,
'--extract-media=../static/Images/' + file_name])
2 weeks ago
# 读取然后修改内容,输出到新的文件
wmf_idx = 0 # wmf索引
img_idx = 0 # 图片索引
with open(temp_markdown, 'r', encoding='utf-8') as f:
for line in f:
line = line.strip()
if not line:
continue
# 跳过图片高度描述行
if line.startswith('height=') and line.endswith('in"}'):
continue
# 使用find()方法安全地检查图片模式
2 weeks ago
is_wmf = line.find("![](") >= 0 and line.find(".wmf") > 0
is_img = line.find("![](") >= 0 and (
2 weeks ago
line.find(".png") > 0 or
line.find(".jpg") > 0 or
line.find(".jpeg") > 0
)
if is_wmf:
content += formula_list[wmf_idx] + "\n"
wmf_idx += 1
elif is_img:
2 weeks ago
# ![](media/image3.png){width="3.1251607611548557in"
2 weeks ago
# height="3.694634733158355in"}
2 weeks ago
# ![](../static/Images/01b20e04085e406ea5375791da58a60f/media/image3.png){width="3.1251607611548557in"
pos = line.find(")")
q = line[:pos + 1]
2 weeks ago
img_idx += 1
2 weeks ago
content += q + "\n"
2 weeks ago
else:
2 weeks ago
content += line.strip().replace("**", "") + "\n"
2 weeks ago
# 删除临时文件 output_file
2 weeks ago
os.remove(temp_markdown)
2 weeks ago
return content