multiple .doc to .docx file conversion using python

前端 未结 5 1917
长发绾君心
长发绾君心 2020-12-10 16:36

I want to convert all the .doc files from a particular folder to .docx file.

I tried using the following code,

import subprocess
import os
for filena         


        
5条回答
  •  星月不相逢
    2020-12-10 17:20

    I prefer to use the glob module for tasks like that. Put this in a file doc2docx.py. To make it executable, set chmod +x. And optionally put that file in your $PATH as well, to make it available "everywhere".

    #!/usr/bin/env python
    
    import glob
    import subprocess
    
    for doc in glob.iglob("*.doc"):
        subprocess.call(['soffice', '--headless', '--convert-to', 'docx', doc])
    

    Though ideally you'd leave the expansion to the shell itself, and call doc2docx.py with the files as arguments, like doc2docx.py *.doc:

    #!/usr/bin/env python
    
    import subprocess
    import sys
    
    if len(sys.argv) < 2:
        sys.stderr.write("SYNOPSIS: %s file1 [file2] ...\n"%sys.argv[0])
    
    for doc in sys.argv[1:]:
        subprocess.call(['soffice', '--headless', '--convert-to', 'docx', doc])
    

    As requested by @pyd, to output to a target directory myoutputdir use:

    #!/usr/bin/env python
    
    import subprocess
    import sys
    
    if len(sys.argv) < 2:
        sys.stderr.write("SYNOPSIS: %s file1 [file2] ...\n"%sys.argv[0])
    
    for doc in sys.argv[1:]:
        subprocess.call(['soffice', '--headless', '--convert-to', 'docx', '--outdir', 'myoutputdir', doc])
    

提交回复
热议问题