Skip to content

Commit f64a510

Browse files
committed
support custom docx template file for styling
1 parent 5cf951b commit f64a510

File tree

4 files changed

+51
-4
lines changed

4 files changed

+51
-4
lines changed

tools/md_to_docx/md_to_docx.py

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
import logging
2-
from typing import Generator
2+
import os
3+
from pathlib import Path
4+
from tempfile import NamedTemporaryFile
5+
from typing import Generator, Optional
36

47
from dify_plugin import Tool
58
from dify_plugin.entities.tool import ToolInvokeMessage
9+
from dify_plugin.file.file import File
610

711
from tools.utils.file_utils import get_meta_data
812
from tools.utils.mimetype_utils import MimeType
@@ -19,8 +23,30 @@ def _invoke(self, tool_parameters: dict) -> Generator[ToolInvokeMessage, None, N
1923
"""
2024
# get parameters
2125
md_text = get_md_text(tool_parameters, is_strip_wrapper=True)
26+
docx_template_file: Optional[File] = tool_parameters.get("docx_template_file")
27+
temp_pptx_template_file_path: Optional[str] = None
28+
if docx_template_file and not isinstance(docx_template_file, File):
29+
raise ValueError("Not a valid file for pptx template file")
30+
2231
try:
23-
result_file_bytes = pandoc_convert_file(md_text, "docx")
32+
if docx_template_file:
33+
temp_pptx_template_file = NamedTemporaryFile(delete=False)
34+
temp_pptx_template_file.write(docx_template_file.blob)
35+
temp_pptx_template_file.close()
36+
temp_pptx_template_file_path = temp_pptx_template_file.name
37+
38+
current_script_folder = os.path.split(os.path.realpath(__file__))[0]
39+
if temp_pptx_template_file_path:
40+
template_docx_file = temp_pptx_template_file_path
41+
else:
42+
template_docx_file = f"{current_script_folder}/template/docx_template.docx"
43+
44+
# Options for pandoc
45+
# https://pandoc.org/MANUAL.html#options
46+
extra_args = [
47+
"--reference-doc", template_docx_file,
48+
]
49+
result_file_bytes = pandoc_convert_file(md_text, dst_format="docx", extra_args=extra_args)
2450
yield self.create_blob_message(
2551
blob=result_file_bytes,
2652
meta=get_meta_data(
@@ -32,3 +58,6 @@ def _invoke(self, tool_parameters: dict) -> Generator[ToolInvokeMessage, None, N
3258
self.logger.exception("Failed to convert markdown text to DOCX file")
3359
yield self.create_text_message(f"Failed to convert markdown text to DOCX file, error: {str(e)}")
3460
raise e
61+
finally:
62+
if temp_pptx_template_file_path:
63+
Path(temp_pptx_template_file_path).unlink()

tools/md_to_docx/md_to_docx.yaml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,17 @@ parameters:
2020
en_US: Markdown text
2121
zh_Hans: Markdown格式文本
2222
form: llm
23+
- name: docx_template_file
24+
type: file
25+
required: false
26+
label:
27+
en_US: DOCX Template File
28+
zh_Hans: DOCX 模板文件
29+
human_description:
30+
en_US: Optional docx template file for styling。Use "Home"-"Styles Pane" in Microsoft Word to edit styles of the docx file.
31+
zh_Hans: 可选的docx模板文件,用于样式控制。使用Microsoft Word中“开始”-“样式窗格”对docx文件的样式进行编辑。
32+
llm_description: An optional .pptx file that serves as a template for the generated presentation
33+
form: llm
2334
- name: output_filename
2435
type: string
2536
required: false
13.1 KB
Binary file not shown.

tools/utils/pandoc_utils.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,23 @@
11
from pathlib import Path
22
from tempfile import NamedTemporaryFile
3+
from typing import Optional
34

45
from pypandoc import convert_file, convert_text
56

67

7-
def pandoc_convert_file(md_text: str, dst_format: str) -> bytes:
8+
def pandoc_convert_file(md_text: str, dst_format: str, extra_args: Optional[list[str]]) -> bytes:
89
with NamedTemporaryFile(suffix=".md", delete=True) as md_file:
910
md_file.write(md_text.encode("utf-8"))
1011
md_file.flush()
1112

1213
with NamedTemporaryFile(suffix=f".{dst_format}", delete=True) as target_file:
13-
convert_file(source_file=md_file.name, format="markdown", to=dst_format, outputfile=target_file.name)
14+
convert_file(
15+
source_file=md_file.name,
16+
format="markdown",
17+
to=dst_format,
18+
outputfile=target_file.name,
19+
extra_args=extra_args,
20+
)
1421
target_file.flush()
1522
return Path(target_file.name).read_bytes()
1623

0 commit comments

Comments
 (0)