Skip to content

ExportDocx

export_md_to_docx(input_text, file_name='output.docx', output_file_path='.')

Export Markdown text to docx file Args: input_text (str): Markdown text file_name (str): Name of the output docx file. Default to 'output.docx'. output_file_path (str): Path to the output docx file. Default to current directory.

Source code in src/gemini-cli/export/ExportDocx.py
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
def export_md_to_docx(input_text: str, file_name='output.docx', output_file_path: str = '.'):
    """Export Markdown text to docx file
    Args:
        input_text (str): Markdown text
        file_name (str): Name of the output docx file. Default to 'output.docx'.
        output_file_path (str): Path to the output docx file. Default to current directory.
    """
    # Check if Pandoc is installed
    if not _is_pandoc_installed():
        console.log("Pandoc is not installed. Installing Pandoc is required to export to docx format.")
        try:
            console.log("Installing Pandoc...", style="bold")
            pydoc.download_pandoc()
            console.log("Pandoc installation complete.")
        except Exception as e:
            console.log(f"Error installing Pandoc: {e}\nPlease install Pandoc manually and try again.",
                        style="bold red")
            return None

    # check output file path end with / or not if yes then remove it
    if output_file_path.endswith('/'):
        output_file_path = output_file_path[:-1]

    # check filename ends with .docx or not if not then add it
    if not file_name.endswith('.docx'):
        file_name = file_name + '.docx'
    # Convert and save the output to the specified file
    try:
        with console.status("Exporting to docx...", spinner="dots"):
            pydoc.convert_text(input_text, 'docx', format='md', outputfile=f'{output_file_path}/{file_name}')
        console.log(f"Markdown text exported to docx: {output_file_path}/{file_name}", style="bold green")
    except Exception as e:
        console.log(f"Error converting to docx: {e}", style="bold red")
        return None