multiple .doc to .docx file conversion using python

前端 未结 5 1912
长发绾君心
长发绾君心 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:25

    If you don't like to rely on sub-process calls, here is the version with COM client. It is useful if you are targeting windows users without LibreOffice installed.

    #!/usr/bin/env python
    
    import glob
    import win32com.client
    
    word = win32com.client.Dispatch("Word.Application")
    word.visible = 0
    
    for i, doc in enumerate(glob.iglob("*.doc")):
        in_file = os.path.abspath(doc)
        wb = word.Documents.Open(in_file)
        out_file = os.path.abspath("out{}.docx".format(i))
        wb.SaveAs2(out_file, FileFormat=16) # file format for docx
        wb.Close()
    
    word.Quit()
    

提交回复
热议问题