合并两个PDF [英] Merging two PDFs

查看:118
本文介绍了合并两个PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

import PyPDF2 
import glob
import os
from fpdf import FPDF
import shutil

class MyPDF(FPDF): # adding a footer, containing the page number
    def footer (self):
        self.set_y(-15)
        self.set_font("Arial", Style="I", size=8)
        pageNum = "page %s/{nb}" % self.page_no()
        self.cell(0,10, pageNum, align="C")


if __name__ == "__main__":
    os.chdir("pathtolocation/docs/") # docs location
    os.system("libreoffice --headless --invisible --convert-to pdf *") # this converts everything to pdf
    for file in glob.glob("*"):
        if file not in glob.glob("*.pdf"):
            shutil.move(file,"/newlocation") # moving files we don't need to another folder

    # adding the cover and footer
    path = open(file, 'wb')
    path2 = open ('/pathtocover/cover.pdf')
    merger = PyPDF2.PdfFileMerger()
    pdf = MyPDF()

    for file in glob.glob("*.pdf"):
        pdf.footer()
        merger.merge(position=0, fileobj=path2)
        merger.merge(position=0, fileobj=path)
        merger.write(open(file, 'wb'))

此脚本将转换为pdf,在pdf和页脚中添加一个包含页码的封面,修复了一些问题,现在我最后一次运行它以查看它是否有效,这花费了太多时间,没有错误,做了我做错了什么,或者合并和添加页脚需要那么长时间吗?我正在处理3个文件,它转换得非常快.

This script converts to pdf, add a cover to the pdf and footer containing the page number, fixed some stuff and now I run it for the last time to see if it's working, it's taking too much time, no error, did I do something wrong or does it need that long to merge and add footers? I'm working with 3 files, and it converted them so fast.

异常输出

convert /home/projects/convert-pdf/docs/sample (1).doc ->
/home/projects/convert-pdf/docs/sample (1).pdf using writer_pdf_Export

所以它正在转换和移动,我认为问题出在这里

so it is converting and moving, I think the problem is somewhere here

   for file in glob.glob("*.pdf"):
        pdf.footer()
        merger.merge(position=0, fileobj=path2)
        merger.merge(position=0, fileobj=path)
        merger.write(open(file, 'wb'))

由于我正在尝试将position=0position=0合并,因此不确定这一点

Since I'm trying to merge position=0 with position=0, not sure about it though

推荐答案

作为注释,这实际上更好,但是我想显示代码.您需要在其中添加一些try块来捕获任何错误-这是您可以做的超级基本操作.

This is actually better as a comment, but I want to show code. You need to add some try blocks in there to catch any errors - here is something super basic you coudld do.

import PyPDF2 
import glob
import os
from fpdf import FPDF
import shutil

class MyPDF(FPDF): # adding a footer, containing the page number
    def footer (self):
      try:
        self.set_y(-15)
        self.set_font("Arial", Style="I", size=8)
        pageNum = "page %s/{nb}" % self.page_no()
        self.cell(0,10, pageNum, align="C")
      except Exception, err:
        print "Error applying footer: {}".format(err)


if __name__ == "__main__":

  try:
    os.chdir("pathtolocation/docs/") # docs location
    os.system("libreoffice --headless --invisible --convert-to pdf *") # this converts everything to pdf
    for file in glob.glob("*"):
        if file not in glob.glob("*.pdf"):
            shutil.move(file,"/newlocation") # moving files we don't need to another folder

    # adding the cover and footer
    path = open(file, 'wb')
    path2 = open ('/pathtocover/cover.pdf')
    merger = PyPDF2.PdfFileMerger()
    pdf = MyPDF()
  except Exception, err:
    print "error setting up the pdf: {}".format(err)

    for file in glob.glob("*.pdf"):
      try:
        pdf.footer()
        merger.merge(position=0, fileobj=path2)
        merger.merge(position=0, fileobj=path)
        merger.write(open(file, 'wb'))
      except Exception, err:
        print "Error processing glob: {}".format(err)

这篇关于合并两个PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆