栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

python将doc文件转换docx

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

python将doc文件转换docx

一:python将doc文件批量转换docx:

# -*- coding:utf-8 -*-
# @Author : yyzhang
import os
import time
from win32com import client

def doc_to_docx(list_dir,save_file):
    word = client.Dispatch("Word.Application")  # 打开word应用程序
    filename_list = [i for i in list_dir if i.split(".")[-1] == "doc"]
    # filename_list=[os.path.join(folder,j) for j in li ]
    # print(filename_list)
    # time.sleep(10)
    try:
        for file in filename_list:
            print("开始转换:",file)
            # print(file)
            # 将doc的文件名换成后缀为docx的文件
            name =  os.path.splitext(file)[0] + '.docx'
            # 将我们的docx与文件保存位置拼接起来,获得绝对路径
            out_name = os.path.join(save_file,name)  #
            print("测试后:",name)
            print("转换后:",out_name)
            # out_file.append(out_name)
            file_path=os.path.join(folder,file)
            doc = word.Documents.Open(file_path)  # 打开word文件
            # doc.SaveAs("{}".format(out_name), 12)  # 另存为后缀为".docx"的文件,其中参数12或16指docx文件
            doc.SaveAs("{}".format(out_name), 12, False, "", True, "", False,
                       False, False,
                       False)  # 转换后的文件,12代表转换后为docx文件
            doc.Close()  # 关闭原来word文件
    except Exception as e:
        print(e)
    word.Quit()

if __name__ == "__main__":
    # 支持文件夹批量导入
    folder = 'C:\Users\Desktop\DOC_docx\doc-file'
    list_dir=os.listdir(folder)
    # print(list_dir)
    out_dir = 'C:\Users\Desktop\DOC_docx\doc-file1'
    doc_to_docx(list_dir,out_dir)

二:python将docx文件转换pdf:

import comtypes.client
import win32com
import os
import sys

try:
    from comtypes import client
except ImportError:
    client = None

try:
    from win32com.client import constants, gencache
except ImportError:
    constants = None
    gencache = None


def doc2pdf(docPath, pdfPath):
    docPathTrue = os.path.abspath(docPath)
    # word = gencache.EnsureDispatch('Word.Application')
    word = win32com.client.DispatchEx('Word.Application')
    doc = word.Documents.Open(docPathTrue, ReadOnly=1)
    doc.ExportAsFixedFormat(pdfPath,
                            constants.wdExportFormatPDF,
                            Item=constants.wdExportDocumentWithMarkup,
                            CreateBookmarks=constants.wdExportCreateHeadingBookmarks)
    word.Quit(constants.wdDoNotSaveChanges)
doc2pdf(r"C:UsersDesktopDOC_docxnew_docx语音专线协议.docx",r"C:UsersDesktopDOC_docx语音专线协议.pdf")

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/991163.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号