python批量轉換word和excel格式

python批量轉換word和excel格式

實現的目標

批量將目錄(包括子目錄)下的全部doc和xls文件,轉換爲docx和xlsx格式。javascript

用到的python模塊

pip install pywin32java

腳本內容

import os
import os.path
import win32com.client as win32
import threading
#解決pywintypes.com_error報錯
import pythoncom

## 根目錄
rootdir = u'D:\舊版文檔'

def xls2xlsx():
    #解決pywintypes.com_error報錯
    pythoncom.CoInitialize()
    excel = win32.gencache.EnsureDispatch('Excel.Application')
    #遍歷文檔目錄中的全部xls文件
    for parent, dirnames, filenames in os.walk(rootdir):
        for fn in filenames:
            filedir = os.path.join(parent, fn)
            if fn.endswith('.xls'):
                #打印處理的文件名
                print(filedir)
                try:
                    #打開xls文件
                    wb = excel.Workbooks.Open(filedir)
            # xlsx: FileFormat=51
            # xls:  FileFormat=56,
                    try:
                        #保存爲xlsx文件,51表示xslx文件格式
                        wb.SaveAs(filedir.replace('xls', 'xlsx'), FileFormat=51)
                        wb.Close()
                        #刪除xls文件
                        os.remove(filedir)
                    except Exception as e:
                        print(e)
                except Exception as e:
                    print(e)
    excel.Application.Quit()
    print('xls end')

def doc2docx():
    pythoncom.CoInitialize()
    word = win32.Dispatch("Word.Application")
    # 三個參數:父目錄;全部文件夾名(不含路徑);全部文件名
    #獲取全部doc文件
    for parent, dirnames, filenames in os.walk(rootdir):
        for fn in filenames:
            filedir = os.path.join(parent, fn)
            if fn.endswith('.doc'):
                #打印處理的文件名
                print(filedir)
                try:
                    #打開全部doc文件
                    doc = word.Documents.Open(filedir)
                    try:
                        #另存爲後綴爲".docx"的文件,其中參數12指docx文件
                        doc.SaveAs("{}x".format(filedir),12)
                        doc.Close()
                        #刪除doc文件
                        os.remove(filedir)
                    except Exception as e:
                        print(e)
                except Exception as e:
                    print(e)
    word.Quit()
    print('doc end')

def main():
    #啓用多線程xls和doc文件分別另存爲
    threads = []
    t1 = threading.Thread(target=xls2xlsx,args=())
    t2 = threading.Thread(target=doc2docx,args=())
    threads.append(t1)
    threads.append(t2)
    for t in threads:
        t.setDaemon(True)
        t.start()
    for i in threads:
        i.join()
    print("全部任務完成")
        
if __name__ == '__main__':
    main()