multiple .doc to .docx file conversion using python

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

    based on dshefman's code,

    import re
    import os
    import sys
    import win32com.client as win32
    from win32com.client import constants
    
    # Get path from command line argument
    ABS_PATH = sys.argv[1]
    
    def save_as_docx(path):
        # Opening MS Word
        word = win32.gencache.EnsureDispatch('Word.Application')
        doc = word.Documents.Open(path)
        doc.Activate ()
    
        # Rename path with .docx
        new_file_abs = os.path.abspath(path)
        new_file_abs = re.sub(r'\.\w+$', '.docx', new_file_abs)
    
        # Save and Close
        word.ActiveDocument.SaveAs(new_file_abs, FileFormat=constants.wdFormatXMLDocument)
        doc.Close(False)
    
    def main():
        source = ABS_PATH
    
        for root, dirs, filenames in os.walk(source):
            for f in filenames:
                filename, file_extension = os.path.splitext(f)
    
                if file_extension.lower() == ".doc":
                    file_conv = os.path.join(root, f)
                    save_as_docx(file_conv)
                    print("%s ==> %sx" %(file_conv,f))
    
    if __name__ == "__main__":
        main()
    

提交回复
热议问题