首页 > 解决方案 > lowriter Bash 脚本将所有 doc 就地转换为 pdf

问题描述

所以..我的任务是将一堆 *.doc 文件转换为 *.pdf 使用lowriter

我想做的是就地执行此操作,但由于无法使用 执行此操作lowriter,我想我会捕获原始文件和路径,捕获转换,然后将转换后的文件移动到原始路径,并且然后删除原来的 *.doc

问题是我的sed和或awk充其量是弱的;)所以我无法弄清楚如何从输出中“捕获”转换后的文件名。

我的代码:

#!/bin/bash

FILES=/my/path/**/*.doc

shopt -s globstar

for f in $FILES; do

    the_file=$f;
    the_orig_dir=$(dirname "$the_file") ;

    converted=$(lowriter --headless --convert-to pdf "$the_file");
    
    echo $converted;
done;

输出是:

convert /my/path/Archives/Ally/Heavenly Shop.doc -> /my/Heavenly Shop.pdf using filter : writer_pdf_Export
convert /my/path/Archives/Ally2/Solutions Shop.doc -> /my/Solutions Shop.pdf using filter : writer_pdf_Export
convert /my/path/Archives/Ally3/Xpress Shop.doc -> /my/Xpress Shop.pdf using filter : writer_pdf_Export

我需要做的是->:. 我只是不知道我该怎么做。有人可以告诉我吗?

标签: bashawksedlibreofficelibreoffice-writer

解决方案


对您提出的问题的快速回答是,这将使用任何 sed:

sed 's/.*-> \(.*\) using filter :.*/\1/'

但我不确定你是否真的需要这样做。根据您发布的内容和您在问题下的评论,我认为您真正需要的是:

#!/usr/bin/env bash

shopt -s globstar

docPaths=( /my/path/**/*.doc )

for docPath in "${docPaths[@]}"; do

    pdfPath=$(basename "$docPath" '.doc')'.pdf'

    lowriter --headless --convert-to pdf "$docPath"
    
    printf '%s\n' "$pdfPath"

done

推荐阅读