python word转pdf pdf批量合并

工作平台:window10

工作环境:python2.7.13

python 开发包:pywin32 /pypdf2  

开发包下载命令:pip install pywin32/pip install pypdf2

若出现pywin32无法安装的情况,请下载whl包:https://www.lfd.uci.edu/~gohlke/pythonlibs/#pywin32,使用管理员权限进行安装

源代码:

# -*- coding:utf-8 -*-
# doc2pdf.py: python script to convert doc to pdf with bookmarks!
# Requires Office
# Requires python for win32 extension
# requires python for pypdf2
import os
from win32com.client import Dispatch, constants, gencache
from PyPDF2 import PdfFileWriter,PdfFileReader,PdfFileMerger
#word批量转pdf
def word2pdf_patch(fileIn_path,fileOut_path):
    # from config import REPORT_DOC_PATH,REPORT_PDF_PATH
    fileIn_path = fileIn_path.decode('utf-8')
    path_list = os.listdir(fileIn_path)
    for item in path_list:
        print(item)
        rc = word2pdf(os.path.join(fileIn_path,item))
#word转pdf
def word2pdf(input):
    output = input
    try:
        print input, output
        # enable python COM support for Word 2007
        # this is generated by: makepy.py -i "Microsoft Word 12.0 Object Library"
        gencache.EnsureModule('{00020905-0000-0000-C000-000000000046}', 0, 8, 4)
        # 开始转换
        w = Dispatch("Word.Application")
        try:
            doc = w.Documents.Open(input, ReadOnly=1)
            doc.ExportAsFixedFormat(output, constants.wdExportFormatPDF, \
                                    Item=constants.wdExportDocumentWithMarkup,
                                    CreateBookmarks=constants.wdExportCreateHeadingBookmarks)
        except:
            print ' exception'
        finally:
            w.Quit(constants.wdDoNotSaveChanges)

        if os.path.isfile(output):
            print 'translate success'
            return True
        else:
            print 'translate fail'
            return False
    except:
        print ' exception'
        return -1
#合并pdf为一个文件
def merge_pdf(infnList, outfn):
    pdf_output = PdfFileWriter()
    for infn in infnList:
        pdf_input = PdfFileReader(open(infn, 'rb'))
        # 获取 pdf 共用多少页
        page_count = pdf_input.getNumPages()
        print(page_count)
        for i in range(page_count):
            pdf_output.addPage(pdf_input.getPage(i))
    pdf_output.write(open(outfn, 'wb'))

if __name__ == '__main__':
    print 'starting dealing'

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值