I usually output text from my shell to file using >. I am trying to find an equivalent process that outputs a MS Word document (.doc or .docx).

I was thinking to use libreoffice and came up with this using the convert-to command:

echo "texthere" > tt.txt;   soffice --convert-to doc tt.txt

That is, I am first creating a plain txt document and then convert it to doc in a second step.

Question: Is there a one-step solution?

E.g. along the lines of:

echo "texthere" | command_to_output_as_doc filename.doc

Thanks! (I'm using GNU bash, version 4.3.48(1) on Elementary OS)

  • if soffice can read from standard input with the - argument ,that might work. – DopeGhoti Jul 21 '17 at 18:38
  • 3
    Just creating your output file as .doc or .docx should work. echo "texthere" > tt.docx – Jesse_b Jul 21 '17 at 18:43
  • @Jesse_b Nice, that was a lot easier than I thought, thanks! Not saying I quite understand it -- I always figured .docx was such as complicated format. – patrick Jul 21 '17 at 18:46
  • @patrick, jesse_b's solution does not change format. It just names your text file as something.docx. – user4556274 Jul 21 '17 at 19:46
  • 2
    If you are only sending unformatted plaintext to the file there is no need for it to be "converted" (as there is nothing to convert). The file will be "converted" when you open it with microsoft word but no changes will actually be made to the file unless you add some formatted text to it. – Jesse_b Jul 21 '17 at 19:55

I believe you might be able to use pandoc. It reads from stdin and can output to a variety of formats

  • Thanks for your answer! Do you see any issues with the approach suggested by Jesse_b above? It seems to produce legible docx format for me. – patrick Jul 21 '17 at 18:52
  • 1
    if you need or want any kind of formatting (bold, italics, tables, headings, etc) in the word doc, plain-text without markup won't do the job. It's easy to generate markdown from a shell script (it's just plain text with markup characters like # and * embedded, same as is used here on U&L - pandoc understands several 'dialects' of markdown and several other input formats too) and then process it with pandoc to produce a .docx file (or .pdf or open-office .odt, or epub, and several others). – cas Jul 22 '17 at 2:47

This is just to add to the answer by @rcjohnson after testing it. Pandoc does indeed do a very good job of converting various input to docx format, including formatted text.

For instance, the following Python script (saved as pandoctest.py)

print "Here we have **bold** and _italics_ and `code`"

Can be run as follows:

python pandoctest.py | pandoc --from markdown_github --to docx -o testdoc.docx

(indicating that the printout is to be read as Markdown, then saved as docx to testdoc.docx; unless an input file is specified, Pandoc reads from stdin)

This produces a Word document with formatting as shown:

Here we have bold and italics and code

Pandoc has a lot more options for pdfs, different Markdown flavors, etc.

  • Why use Python when you could use echo? echo 'Here we have **bold** and _italics_ and code' – wjandrea Feb 7 at 23:26
  • 1
    Agreed, this was more of an example string to illustrate how you can use output e.g. of existing scripts in general and more complex strings – patrick Feb 8 at 13:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.