Parallel Document Conversion ODT > PDF Libreoffice

后端 未结 6 1582
别跟我提以往
别跟我提以往 2021-02-05 22:41

I am converting hundreds of ODT files to PDF files, and it takes a long time doing one after the other. I have a CPU with multiple cores. Is it possible to use bash or python to

6条回答
  •  粉色の甜心
    2021-02-05 23:12

    Since the author already introduced Python as a valid answer:

    import subprocess
    import os, glob
    from multiprocessing.dummy import Pool    # wrapper around the threading module
    
    def worker(fname, dstdir=os.path.expanduser("~")):
        subprocess.call(["libreoffice", "--headless", "--convert-to", "pdf", fname],
                        cwd=dstdir)
    
    pool = Pool()
    pool.map(worker, glob.iglob(
            os.path.join(os.path.expanduser("~"), "*appsmergeme.odt")
        ))
    

    Using a thread pool instead of a process pool by multiprocessing.dummy is sufficient because new processes for real parallelism are spawn by subprocess.call() anyway.

    We can set the command as well as the current working directory cwd directly. No need to load a shell for each file for just doing that. Furthermore, os.path enables cross-platform interoperability.

提交回复
热议问题