-
Notifications
You must be signed in to change notification settings - Fork 1
/
MD_Translate.py
43 lines (40 loc) · 1.44 KB
/
MD_Translate.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
from Split_MD import process_markdown
import pypandoc
import os
import time
def Process_MD(
md_file: str, translate: callable, thread: int = 10, output_path: str = "./Output"
):
print(f"Processing markdown file: {md_file}")
with open(md_file, "r", encoding="utf-8") as f:
input_md = f.read()
output_md = process_markdown(
input_markdown=input_md, translate=translate, thread=thread
)
output_md_path = os.path.join(
output_path,
".".join(os.path.basename(md_file).split(".")[:-1])
+ "_translated_"
+ time.strftime("%Y%m%d_%H%M%S")
+ ".md",
)
os.makedirs(output_path, exist_ok=True)
with open(output_md_path, "w", encoding="utf-8") as f:
f.write(output_md)
print(f"Translated markdown saved to {output_path}")
print("Trying ranslating markdown to docx...")
try:
output_docx_path = output_md_path.replace(".md", ".docx")
reference_docx = os.path.abspath("reference.docx")
extra_args = [f"--resource-path={output_path}"]
if os.path.exists(reference_docx):
extra_args.append(f"--reference-doc={reference_docx}")
pypandoc.convert_file(
output_md_path,
"docx",
outputfile=output_docx_path,
extra_args=extra_args,
)
print(f"Translated docx saved to {output_docx_path}")
except Exception as e:
print(f"Error converting markdown to docx: {e}")