简体   繁体   English

lowriter Bash 将所有文档就地转换为 pdf 的脚本

[英]lowriter Bash Script to Convert all doc to pdf In-Place

So.. I've been tasked with converting a bunch of *.doc files to *.pdf utilizing lowriter所以.. 我的任务是使用 lowriter 将一堆 *.doc 文件转换为 * lowriter

What I would like to do is do this in place, but since there is no option to do that using lowriter , I figured I would capture the originating file and path, capture the conversion, and then move the converted file to the originating path, and then delete the original *.doc我想做的是就地执行此操作,但由于无法使用lowriter执行此操作,我想我会捕获原始文件和路径,捕获转换,然后将转换后的文件移动到原始路径,然后删除原来的 *.doc

The problem is my sed and or awk is weak at best;) so I cannot figure out how I can "capture" the converted file name from the output.问题是我的sed和或awk是弱的;)所以我不知道如何从 output 中“捕获”转换后的文件名。

My Code:我的代码:

#!/bin/bash #!/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;

and the output is: output 是:

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

What I need to do is capture the path/filename of the converted file after the -> and before the : .我需要做的是在->之后和:之前捕获转换文件的路径/文件名。 I just don't know how I can do this.我只是不知道我该怎么做。 Can someone tell me?有人可以告诉我吗?

The quick answer to the question you asked is that this will work using any sed:您提出的问题的快速回答是,这将使用任何 sed 工作:

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

but I'm not sure you actually need to do that.但我不确定你是否真的需要这样做。 Based on what you posted and your comments under the question I think all you really need is:根据您发布的内容和您在问题下的评论,我认为您真正需要的是:

#!/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
#!/bin/bash

FILES=/my/specific/input/folder/**/*.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");
    
    new_file=$(echo "$converted" | grep -o -P '(?<= -> ).*(?= using filter : )');
    
    new_file_name=$(basename "$new_file");
    
    
    echo "$the_orig_dir/$new_file_name";
    
    
    set -x;
    
    rm -f $the_file;
    
    mv "$new_file" "$the_orig_dir/";
    
    set +x;
    
done;

does what I need it to do做我需要做的事

Following on comment from ed motron, worth mentioning that the libraOffice writer will place the output file in predictable name, based on the --outdir (or current working folder), and the requested conversion (pdf).根据 ed motron 的评论,值得一提的是,libraOffice 编写器将根据--outdir (或当前工作文件夹)和请求的转换 (pdf) 将 output 文件放置在可预测的名称中。 The rules can be used to construct the name of the output file.该规则可用于构造 output 文件的名称。

The above script can simply be written:上面的脚本可以简单的写成:

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

shopt -s globstar

for f in $FILES; do

    lowriter --headless --convert-to pdf "$f"
    converted=$(basename "$f" .doc).pdf
    # Do something with converted ...    
    echo "Output: $converted"
done;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM