How to convert word processor files to different formats.

dos2unix

Well-Known Member
Joined
May 3, 2019
Messages
4,408
Reaction score
4,601
Credits
41,638
This assumes you already have libreoffice installed.
This can be done from the command line without opening the GUI.

Convert .odt to .docx
Code:
libreoffice --headless --convert-to docx yourfile.odt

Convert to .rtf
Code:
libreoffice --headless --convert-to rtf yourfile.odt

Convert to text
Code:
libreoffice --headless --convert-to txt:Text yourfile.odt

Bash batch file conversion
Code:
#!/bin/bash
for file in *.odt; do
libreoffice --headless --convert-to docx "$file"
done

A better odt to txt script.
Code:
#!/bin/bash
# Directory containing .odt files (default: current directory)
INPUT_DIR="."
OUTPUT_DIR="./converted_txt"
# Create output directory if it doesn't exist
mkdir -p "$OUTPUT_DIR"
# Loop through all .odt files in the input directory
for file in "$INPUT_DIR"/*.odt; do
if [[ -f "$file" ]]; then
echo "Converting: $file"
libreoffice --headless --convert-to txt:Text "$file" --outdir "$OUTPUT_DIR"
fi
done
echo "Conversion complete. TXT files saved in: $OUTPUT_DIR"
 


With the current for loop the output will most likely be yourfile.doc.odt. I can show you what to add to avoid that in about half an hour.

Edit: OK, here it is - proper for loop for bulk conversion:

Code:
#!/bin/bash

for file in *.odt;
do
newfile=$(echo "$file" | rev | cut -f 2- -d '.' | rev)
libreoffice --headless --convert-to docx "$newfile".docx;
done
 
Last edited:


Follow Linux.org

Members online


Top