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
with position=0
,但不确定