简体   繁体   English

将 2 个 PDF 页面合并为 1 个页面

[英]Merge 2 PDF pages into 1 page

I would need a solution to combine PDF files.我需要一个解决方案来组合 PDF 文件。 I needed some possibility to add text to the PDF template.我需要一些可能性来向 PDF 模板添加文本。 I have been working so far by creating a PDF file with text using FPDF and merging it using PdfFileMerger.到目前为止,我一直在使用 FPDF 创建带有文本的 PDF 文件并使用 PdfFileMerger 将其合并。 But it happens to me that he creates a new PDF file from both pages, and I should only have the text from the PDF that I generated using FPDF be generated on the PDF template.但我碰巧他从两个页面创建了一个新的 PDF 文件,我应该只在 ZBCD1B68617759B1DFCFF0403A6B5A8D1 模板上生成我使用 FPDF 生成的 PDF 中的文本。

This solution doesn't work for me because it tells me that there is no PyPDF2.pdf even though PyPDF2.pdf is properly installed: Merge two pages into pdf into one page pdf This solution doesn't work for me because it tells me that there is no PyPDF2.pdf even though PyPDF2.pdf is properly installed: Merge two pages into pdf into one page pdf

Here is what I do with FPDF:这是我对 FPDF 所做的:

#work in jupyter

 
from fpdf import FPDF
from pathlib import Path
from PyPDF2 import PdfFileMerger

profile = FPDF()
profile.add_page()
profile.set_font('Arial', 'B', 16)
profile.cell(500, 200, 'Edi Graovac!')
profile.output("temp.pdf", 'F')

merger = PdfFileMerger(strict=False)
profile_pdf=open("temp.pdf", 'rb')
template=open(template_path, 'rb')

merger.append(template)
merger.merge(0,profile_pdf)
merger.write(save_path)
merger.close()

As I've now noted where you reference , the current usage of PyPDF has changed since that code was posted.正如我现在注意到您在哪里引用的那样,PyPDF 的当前用法在发布该代码后发生了变化。 Particularly, in regards to the use of PyPDF2.pdf, as noted here : "PyPDF2.pdf no longer exists. You can import from PyPDF2 directly".特别是关于 PyPDF2.pdf 的使用,如此所述:“PyPDF2.pdf 不再存在。您可以直接从 PyPDF2 导入”。 So if you are trying the solution you referenced, you should be using from PyPDF2 import PageObject , the first two lines relative the reference code should be something like this:因此,如果您正在尝试您引用的解决方案,您应该使用 from PyPDF2 import PageObject ,前两行相对于参考代码应该是这样的:

from PyPDF2 import PdfFileWriter, PdfFileReader
from PyPDF2  import PageObject

Probably relative what you want in your version, more like: from PyPDF2 import PdfFileWriter, PdfFileReader, PdfFileMerger, PageObject .可能相对于您在版本中想要的内容,更像是: from PyPDF2 import PdfFileWriter, PdfFileReader, PdfFileMerger, PageObject Maybe.也许。 I was adding.我在补充。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM