brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: textutil -convert turns Word and RTF files into text or HTML on a Mac

Terminal
~/tips/tip-macos-textutil-convert-docx-to-txt

textutil -convert html report.rtf

Converts documents between txt, html, rtf, docx and other formats from the Terminal, without opening an app.

A folder full of Word documents that I only need as plain text is not a job for a word processor. macOS ships a converter in the Terminal.

textutil -convert txt *.docx
textutil -convert html report.rtf
textutil -convert docx -output letter.docx letter.txt

-convert takes a target format and writes each input file back out in it. The man page lists txt, html, rtf, rtfd, doc, docx, wordml, odt and webarchive. The converted file lands next to the original with a new extension, and the original stays as it is.

A few options are handy. -output sets the name of the first output file, -extension overrides the extension that the format would give, and -stdout prints the result instead of writing a file, which lets you pipe a document straight into grep:

textutil -convert txt -stdout contract.docx | grep -i 'termination'

For plain text and HTML output, -encoding sets the encoding. UTF-8 is the default, and the operation fails if the file cannot be converted to the encoding you asked for.

The caveat is fidelity. It is the same text engine that TextEdit uses, so complex layouts, tracked changes and fancy tables may come out simplified, and it is no replacement for Word when the layout matters. The man page also notes that some options need a connection to the window server, so it may not run in a plain SSH session. The command first appeared in OS X 10.4, so it is on every Mac you will meet.

Further reading: textutil(1) manual

More Terminal tips ioreg -rn AppleSmartBattery | grep CycleCount Terminal can save your windows and tabs as a window group Export a Terminal profile to a .terminal file and import it on another Mac mc hidutil property --set '{"UserKeyMapping":[{"HIDKeyboardModifierMappingSrc":0x700000039,"HIDKeyboardModifierMappingDst":0x700000029}]}' All Terminal tips (116) All topics

Link