python word转pdf,错误:pywintypes.com_error: (-2147023179, ‘接口未知。‘, None, None)

python ,当实现word文档转换未pdf格式时,单个文档单次执行没有任何问题,但是当将一个文件夹下的目录进行批量转换时,遇到错误:pywintypes.com_error: (-2147023179, '接口未知。', None, None)

先说解决办法,在进行一次转换操作后,执行一下time.sleep(3), 问题就解决了。 原因:由于循环,当文件读写速度较快的时候,上一个word未关闭,下一个就打开,导致出现以上问题。 参考来源:https://www.cnblogs.com/vhills/p/8098715.html

 

附上代码:

import os, sys
import shutil
from time import sleep

from win32com.client import gencache, constants


init_path = sys.argv[1]
pdf_init_path = sys.argv[2]


def doc2pdf(word_path, pdf_path):
    """
    make word type file transfer to pdf type
    :param word_path: word file path, include filename
    :type word_path: str
    :param pdf_path: pdf file path ,include pdfname
    :type pdf_path: str
    :return: None
    :rtype: None
    """
    if os.path.exists(word_path):
        word = gencache.EnsureDispatch('Word.Application')
        doc = word.Documents.Open(word_path, ReadOnly=1)
        doc.ExportAsFixedFormat(pdf_path,
                                constants.wdExportFormatPDF,
                                Item=constants.wdExportDocumentWithMarkup,
                                CreateBookmarks=constants.wdExportCreateHeadingBookmarks)
        word.Quit(constants.wdDoNotSaveChanges)
    else:
        print(word_path, 'is not exist')


def batch_doc2pdf(path):
    """
    make all doc or docx document to pdf of src directory ,and store the pdf to dst directory.
    copy other file to dst directory, and keep the original structure of src directory
    :param path: src directory , support filepath
    :type path: str
    :return: None
    :rtype: None
    """
    if os.path.isdir(path):
        dir_path = path
        path_list = os.listdir(path)
        for sub_path in path_list:
            path = dir_path + os.sep + sub_path
            batch_doc2pdf(path)
    else:
        file_path = path
        parent_path, filename = os.path.split(file_path)

        # 创建目标路径
        dst_parent_path = parent_path.replace(init_path, pdf_init_path)
        if os.path.exists(dst_parent_path):
            pass
        else:
            os.makedirs(dst_parent_path)

        if filename.endswith('doc') or filename.endswith('docx'):
            # 生成pdf文件路径
            filename_remove_type = filename.split('.')[:-1]
            pdf = filename_remove_type[0]
            for i in range(1, len(filename_remove_type)):
                pdf += '.' + filename_remove_type[i]
            pdf_name = pdf + '.pdf'
            pdf_path = dst_parent_path + os.sep + pdf_name
            print(pdf_path)

            # 删除已存在的pdf文件
            if os.path.exists(pdf_path):
                os.remove(pdf_path)

            # 将word文档转为pdf格式
            doc2pdf(file_path, pdf_path)
            sleep(3)
            if not os.path.exists(pdf_path):
                print(pdf_name + 'transfer failed')
        else:
            print(filename, 'is not word type file, do not transfer')
            # 复制非doc和docx格式的文档到指定文件夹
            shutil.copy(file_path, dst_parent_path)
            if not os.path.exists(dst_parent_path + os.sep + filename):
                print(print('复制' + filename + ' failed'))


if __name__ == '__main__':
    batch_doc2pdf(init_path)

 

 

 

 

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值