To automate your workflow on Ubuntu 24—converting .odt to Markdown, translating with a local Mistral model, and converting back to .odt—you can create a Bash script or a Python script. Both approaches are efficient, but a Bash script is the simplest for this linear, file-based task.
Here’s how you can do it:
Create a script that chains the commands together. This is ideal for a one-off or repetitive task.
Install dependencies (if not already installed):
bashsudo apt update sudo apt install pandoc libreoffice
Ensure your local Mistral model is accessible via a CLI tool (e.g., curl to a local API or a Python script).
Create the script (e.g., convert_translate.sh):
bash#!/bin/bash # Check if input file is provided if [ "$#" -ne 1 ]; then echo "Usage: $0 <input.odt>" exit 1 fi INPUT_FILE="$1" BASENAME=$(basename "$INPUT_FILE" .odt) OUTPUT_MD="${BASENAME}.md" OUTPUT_TRANSLATED_MD="${BASENAME}_translated.md" OUTPUT_ODT="${BASENAME}_translated.odt" # Step 1: Convert ODT to Markdown echo "Converting $INPUT_FILE to Markdown..." pandoc "$INPUT_FILE" -o "$OUTPUT_MD" || { echo "Pandoc conversion failed"; exit 1; } # Step 2: Translate Markdown using local Mistral model echo "Translating $OUTPUT_MD..." # Replace this with your actual Mistral CLI command. # Example: If using a local API, you might do: # curl -X POST http://localhost:8000/translate -H "Content-Type: text/plain" --data-binary "@$OUTPUT_MD" > "$OUTPUT_TRANSLATED_MD" # Or use a Python script if Mistral is accessed via Python. # For now, assume a placeholder command: mistral-cli translate --input "$OUTPUT_MD" --output "$OUTPUT_TRANSLATED_MD" || { echo "Translation failed"; exit 1; } # Step 3: Convert translated Markdown back to ODT echo "Converting $OUTPUT_TRANSLATED_MD to ODT..." pandoc "$OUTPUT_TRANSLATED_MD" -o "$OUTPUT_ODT" || { echo "Pandoc conversion failed"; exit 1; } echo "Done! Output: $OUTPUT_ODT"
Make the script executable:
bashchmod +x convert_translate.sh
Run the script:
bash./convert_translate.sh your_document.odt
If you need more control (e.g., error handling, logging, or batch processing), use Python.
Install Python dependencies:
bashpip install pypandoc requests
Create the script (e.g., convert_translate.py):
pythonimport os import subprocess import requests def convert_odt_to_md(input_file, output_md): try: subprocess.run(["pandoc", input_file, "-o", output_md], check=True) except subprocess.CalledProcessError as e: print(f"Pandoc conversion failed: {e}") exit(1) def translate_md(input_md, output_translated_md): # Replace with your actual Mistral API call. # Example: Send the file content to a local Mistral API. with open(input_md, "r") as f: text = f.read() # Example: Assuming Mistral is running locally on port 8000 response = requests.post( "http://localhost:8000/translate", headers={"Content-Type": "text/plain"}, data=text ) if response.status_code != 200: print("Translation failed") exit(1) with open(output_translated_md, "w") as f: f.write(response.text) def convert_md_to_odt(input_md, output_odt): try: subprocess.run(["pandoc", input_md, "-o", output_odt], check=True) except subprocess.CalledProcessError as e: print(f"Pandoc conversion failed: {e}") exit(1) if __name__ == "__main__": import sys if len(sys.argv) != 2: print("Usage: python convert_translate.py <input.odt>") exit(1) input_file = sys.argv[1] base = os.path.splitext(input_file)[0] md_file = f"{base}.md" translated_md_file = f"{base}_translated.md" output_odt = f"{base}_translated.odt" convert_odt_to_md(input_file, md_file) translate_md(md_file, translated_md_file) convert_md_to_odt(translated_md_file, output_odt) print(f"Done! Output: {output_odt}")
Run the script:
bashpython3 convert_translate.py your_document.odt
If you have many files, a Makefile can help automate the process for all .odt files in a directory.
Makefile:makefileall: convert translate reconvert convert: pandoc *.odt -o %.md translate: # Add your Mistral translation command here for each .md file mistral-cli translate --input %.md --output %_translated.md reconvert: pandoc *_translated.md -o %_translated.odt
Run with:
bashmake
llama.cpp or a FastAPI server), replace the translation step with the actual command or API call.curl to send text to a local Mistral API:
bashcurl -X POST http://localhost:8000/translate -H "Content-Type: text/plain" --data-binary "@input.md" > output_translated.md
Would you like me to adapt one of these scripts to your exact Mistral CLI/API setup? Or do you need help setting up the Mistral model for local translation?
Vibe can make mistakes. Check answers. Learn more