#!/usr/bin/env python3 """ Markdown to Word document converter script. This script uses pandoc to convert markdown files to Word documents. """ import os import sys import subprocess import argparse from pathlib import Path def convert_markdown_to_docx(input_file, output_file=None, reference_doc=None): """ Convert a markdown file to a Word document using pandoc. Args: input_file (str): Path to the input markdown file output_file (str, optional): Path to the output Word document. If not provided, uses the same name as input with .docx extension reference_doc (str, optional): Path to a reference Word document for styling Returns: bool: True if conversion was successful, False otherwise """ # Validate input file if not os.path.exists(input_file): print(f"Error: Input file '{input_file}' does not exist.") return False if not input_file.lower().endswith(".md") and not input_file.lower().endswith( ".markdown" ): print(f"Warning: Input file '{input_file}' does not have a markdown extension.") # Set output file path if not provided if output_file is None: input_path = Path(input_file) output_file = str(input_path.with_suffix(".docx")) # Build pandoc command cmd = ["pandoc", input_file, "-o", output_file] # Add reference document if provided if reference_doc and os.path.exists(reference_doc): cmd.extend(["--reference-doc", reference_doc]) elif reference_doc: print( f"Warning: Reference document '{reference_doc}' does not exist. Ignoring." ) try: # Run pandoc command result = subprocess.run(cmd, capture_output=True, text=True, check=True) print(f"Successfully converted '{input_file}' to '{output_file}'") return True except subprocess.CalledProcessError as e: print(f"Error: pandoc command failed with return code {e.returncode}") print(f"Error output: {e.stderr}") return False except FileNotFoundError: print("Error: pandoc is not installed or not in PATH") print("Please install pandoc: https://pandoc.org/installing.html") return False def main(): """Main function to handle command line arguments.""" parser = argparse.ArgumentParser( description="Convert markdown files to Word documents" ) parser.add_argument("input", help="Input markdown file path") parser.add_argument("-o", "--output", help="Output Word document path (optional)") parser.add_argument( "-r", "--reference", help="Reference Word document for styling (optional)" ) args = parser.parse_args() success = convert_markdown_to_docx(args.input, args.output, args.reference) sys.exit(0 if success else 1) if __name__ == "__main__": main()