使用python实现pdf和word文件相互转换

工作中经常遇到文件格式之间的转换,使用wps要钱,自己写一个免费的,可以随时使用,以下是代码。

from win32com import client
from pdf2docx import Converter


class DocumentConversion(object):
    def __init__(self, document_path):
        self.document_path = document_path
        pass

    # word文件转成pdf
    def doc_zhuan_pdf(self):
        word = client.Dispatch("Word.Application")
        # 打开word文件
        doc = word.Documents.Open(self.document_path)
        # 另存为pdf
        res = doc.SaveAs("{}.pdf".format(self.document_path[:-4]), 17)
        print(res)
        # 关闭word文件
        doc.Close()
        word.Quit()


    # pdf文件转成word
    def pdf_zhuan_word(self):
        # 读取pdf文档
        pw = Converter(self.document_path)
        # 转化为word文档
        pw.convert("{}.docx".format(self.document_path[:-4]), start=0, end=None)

    def run(self):
        suffix = str(self.document_path).split('.')[1]
        if suffix in ['docx', 'doc']:
            self.doc_zhuan_pdf()
        elif suffix == 'pdf':
            self.pdf_zhuan_word()



DocumentConversion(r'C:\Users\Administrator\Desktop\XXXXX.pdf').run()

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值