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个文件,并且转换速度非常快。
异常输出
convert /home/projects/convert-pdf/docs/sample (1).doc ->
/home/projects/convert-pdf/docs/sample (1).pdf using writer_pdf_Export
所以它正在转换和移动,我认为这个问题就在这里
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=0
与position=0
合并,但不确定
答案 0 :(得分:2)
这实际上更好的评论,但我想显示代码。你需要在那里添加一些try块来捕捉任何错误 - 这是你所做的超级基础。
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)