将目录(和子目录)中的所有 .docx 转换为文本文件并写入新文件的命令行

command line to convert all .docx in a directory (and subdirectories) to text file and write new files

我想将目录(和子目录)中的所有 .docx 文件从命令行转换为文本文件(这样我就可以在这些文件上使用 grep after)。我找到了这个

unzip -p tutu.docx word/document.xml | sed -e 's/<\/w:p>/\n/g; s/<[^>]\{1,\}>//g; s/[^[:print:]\n]\{1,\}//g'

here 效果很好,但它会在终端中发送文件。我想在与 .docx 文件相同的目录中写入新的文本文件(例如 .txt)。我想要一个脚本来递归地执行此操作。

我有这个,使用 antiword,它可以为 .doc 文件做我想要的,但它对 .docx 文件不起作用。

find . -name '*.doc' | while read i; do antiword -i 1 "${i}" >"${i/doc/txt}"; done

我尝试将两者混合使用但没有成功...将不胜感激能够同时执行这两项操作的命令行!

谢谢

以下脚本..

  • 递归地转换 运行 所在目录中的所有 docx 文件(将 find . 中的 . 调整为您希望的起点)
  • 将 txt 文件写入找到 docx 文件的位置

Bash 脚本:

find . -name "*.docx" | while read file; do
    unzip -p $file word/document.xml |
        sed -e 's/<[^>]\{1,\}>//g; s/[^[:print:]]\{1,\}//g' > "${file/docx/txt}"
done

之后你可以 运行 像这样的 grep:

grep -r "some text" --include "*.txt" .

您可以使用pandoc转换docx文件。它不支持 .doc 文件,所以你需要 pandoc 和 antiword。

重用您的 while 循环:

find . -name '*.docx' | while read i; do pandoc --from docx --to plain "${i}" >"${i/docx/txt}"; done