python 實(shí)現(xiàn)docx與doc文件的互相轉(zhuǎn)換
因文件格式要求,需要將docx 與doc文件相互轉(zhuǎn)換,特尋找python代碼,與大家共分享
from win32com import client #轉(zhuǎn)換doc為docx def doc2docx(fn): word = client.Dispatch("Word.Application") # 打開word應(yīng)用程序 #for file in files: doc = word.Documents.Open(fn) #打開word文件 doc.SaveAs("{}x".format(fn), 12)#另存為后綴為".docx"的文件,其中參數(shù)12或16指docx文件 doc.Close() #關(guān)閉原來word文件 word.Quit() #轉(zhuǎn)換docx為doc def docx2doc(fn): word = client.Dispatch("Word.Application") # 打開word應(yīng)用程序 #for file in files: doc = word.Documents.Open(fn) #打開word文件 doc.SaveAs("{}".format(fn[:-1]), 0)#另存為后綴為".docx"的文件,其中參數(shù)0指doc doc.Close() #關(guān)閉原來word文件 word.Quit() docx2doc(u"d:\\python\\1.docx")
如果想轉(zhuǎn)換為其他格式文件,需要在format文件名內(nèi)修改,并用如下save as 參數(shù)
如docx轉(zhuǎn)換為pDf,用如下語句:
doc.SaveAs("{}.pdf".format(fn[:-5]), 17)
需要說明的是:
要安裝OFFICE,如果是使用金山WPS的,則還不能應(yīng)用
補(bǔ)充:python批量將文件夾內(nèi)所有doc轉(zhuǎn)成docx
doc轉(zhuǎn)docx函數(shù)
import os from win32com import client def doc_to_docx(path): if os.path.splitext(path)[1] == ".doc": word = client.Dispatch('Word.Application') doc = word.Documents.Open(path) # 目標(biāo)路徑下的文件 doc.SaveAs(os.path.splitext(path)[0]+".docx", 16) # 轉(zhuǎn)化后路徑下的文件 doc.Close() word.Quit() path = ""#填寫文件夾路徑 doc_to_docx(path)
獲取文件夾下的所有文件的絕對路徑
import os def find_file(path, ext, file_list=[]): dir = os.listdir(path) for i in dir: i = os.path.join(path, i) if os.path.isdir(i): find_file(i, ext, file_list) else: if ext == os.path.splitext(i)[1]: file_list.append(i) return file_list dir_path = "" ext = ".doc" file_list = find_file(dir_path, ext)
源碼
import os from win32com import client def doc_to_docx(path): if os.path.splitext(path)[1] == ".doc": word = client.Dispatch('Word.Application') doc = word.Documents.Open(path) # 目標(biāo)路徑下的文件 doc.SaveAs(os.path.splitext(path)[0]+".docx", 16) # 轉(zhuǎn)化后路徑下的文件 doc.Close() word.Quit() def find_file(path, ext, file_list=[]): dir = os.listdir(path) for i in dir: i = os.path.join(path, i) if os.path.isdir(i): find_file(i, ext, file_list) else: if ext == os.path.splitext(i)[1]: file_list.append(i) return file_list dir_path = "C:\Users\python"#批量轉(zhuǎn)換文件夾 ext = ".doc" file_list = find_file(dir_path, ext) for file in file_list: doc_to_docx(file)
以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。如有錯誤或未考慮完全的地方,望不吝賜教。
相關(guān)文章
TensorFlow打印tensor值的實(shí)現(xiàn)方法
今天小編就為大家分享一篇TensorFlow打印tensor值的實(shí)現(xiàn)方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-07-07Python數(shù)據(jù)可視化探索實(shí)例分享
這篇文章主要介紹了Python數(shù)據(jù)可視化探索實(shí)例分享,數(shù)據(jù)可視化是指用圖形或表格的方式來呈現(xiàn)數(shù)據(jù),關(guān)于更多相關(guān)介紹需要的小伙伴可以參考下面文章的具體內(nèi)容2022-05-05超詳細(xì)注釋之OpenCV實(shí)現(xiàn)視頻實(shí)時人臉模糊和人臉馬賽克
這篇文章主要介紹了OpenCV實(shí)現(xiàn)視頻實(shí)時人臉模糊和人臉馬賽克,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-09-09Python函數(shù)必須先定義,后調(diào)用說明(函數(shù)調(diào)用函數(shù)例外)
這篇文章主要介紹了Python函數(shù)必須先定義,后調(diào)用說明(函數(shù)調(diào)用函數(shù)例外),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-06-06python3模擬百度登錄并實(shí)現(xiàn)百度貼吧簽到示例分享(百度貼吧自動簽到)
這篇文章主要介紹了python3模擬百度登錄并實(shí)現(xiàn)百度貼吧簽到示例,需要的朋友可以參考下2014-02-02