婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av

主頁 > 知識(shí)庫 > python 實(shí)現(xiàn)docx與doc文件的互相轉(zhuǎn)換

python 實(shí)現(xiàn)docx與doc文件的互相轉(zhuǎn)換

熱門標(biāo)簽:北京外呼電銷機(jī)器人招商 云南地圖標(biāo)注 電銷機(jī)器人 金倫通信 400電話 申請(qǐng) 條件 crm電銷機(jī)器人 汕頭電商外呼系統(tǒng)供應(yīng)商 鄭州智能外呼系統(tǒng)中心 南京crm外呼系統(tǒng)排名 賓館能在百度地圖標(biāo)注嗎

因文件格式要求,需要將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)

獲取文件夾下的所有文件的絕對(duì)路徑

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)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。

您可能感興趣的文章:
  • python-docx文件路徑問題的解決方案
  • python-docx文件定位讀取過程(嘗試替換)
  • python操作docx寫入內(nèi)容,并控制文本的字體顏色
  • 基于python的docx模塊處理word和WPS的docx格式文件方式
  • Python使用docx模塊實(shí)現(xiàn)刷題功能代碼
  • python-docx 頁面設(shè)置詳解

標(biāo)簽:昆明 錫林郭勒盟 文山 西寧 石家莊 梅州 懷化 浙江

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《python 實(shí)現(xiàn)docx與doc文件的互相轉(zhuǎn)換》,本文關(guān)鍵詞  python,實(shí)現(xiàn),docx,與,doc,文件,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《python 實(shí)現(xiàn)docx與doc文件的互相轉(zhuǎn)換》相關(guān)的同類信息!
  • 本頁收集關(guān)于python 實(shí)現(xiàn)docx與doc文件的互相轉(zhuǎn)換的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    主站蜘蛛池模板: 自治县| 宝兴县| 香港 | 盘山县| 松江区| 库伦旗| 会泽县| 盐边县| 阿巴嘎旗| 延寿县| 平乡县| 基隆市| 闽清县| 和林格尔县| 连州市| 磴口县| 府谷县| 台湾省| 清新县| 长沙市| 称多县| 松滋市| 屏东市| 胶南市| 安顺市| 德庆县| 兴隆县| 荥阳市| 越西县| 雷波县| 辰溪县| 旌德县| 繁峙县| 丹巴县| 嘉善县| 桑日县| 东光县| 鲁山县| 内黄县| 平舆县| 女性|