英文:
Add text to multiple page long PDF using Python
问题
以下是翻译好的部分:
我的情况
嗨,我相对新手使用Python进行编程,目前正在开发一个“PDF-Writer”,它应该能够读取PDF文件,然后在其上绘制特定的字符串以及其他一些内容。我正在使用canvas和PyPDF2来实现这个功能,目前工作正常。
我的问题是与以下问题相关:
(https://stackoverflow.com/questions/1180115/add-text-to-existing-pdf-using-python#comment77571019_17538003)
其他人已在帖子下发表了评论,但没有提供答案,这就是为什么我提出这个问题。我本来想在帖子下发表评论,但遗憾的是,我没有足够的声望来评论。但我认为通过提出一个新问题来解决这个问题也是可以的。
我的问题
- 我的第一个问题是我只能在文档的第一页上写入内容。我希望能够在文档的所有页面上写入内容。
- 我的第二个问题是文档的第一页也是某种原因下的最后一页。
这是我的代码:
from PyPDF2 import PdfFileWriter, PdfFileReader
import io
from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import letter
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont
from reportlab.lib.units import cm
file = input("给出文件路径 >> ")
packet = io.BytesIO()
can = canvas.Canvas(packet, pagesize=letter)
# 注册字体
pdfmetrics.registerFont(TTFont('Arial', 'Arial.ttf'))
can.setFont(psfontname="Arial", size=10)
# 创建一个新字符串
can.drawString(93, 54.5, "2022-12-21")
# 创建一个白色矩形
can.setFillColorRGB(254, 254, 254)
can.rect(92.5, 65, 2*cm, 0.34*cm, fill=1, stroke=0)
# 保存PDF
can.save()
# 移动到StringIO缓冲区的开头
packet.seek(0)
# 使用Reportlab创建一个新的PDF
new_pdf = PdfFileReader(packet)
# 读取现有的PDF
existing_pdf = PdfFileReader(open(file, "rb"))
output = PdfFileWriter()
# 将“水印”(即新的PDF)添加到现有页面
page = existing_pdf.pages[0]
page.merge_page(new_pdf.pages[0])
output.append_pages_from_reader(existing_pdf)
output.add_page(page)
# 最后,将“output”写入实际文件
output_stream = open("destination3.pdf", "wb")
output.write(output_stream)
output_stream.close()
print("PDF已创建!")
我尝试过的
嗯,我在代码中添加了output.append_pages_from_reader(existing_pdf)
,这导致了问题。现在我正在寻找一个有效的工作代码
英文:
My Situation
Hi, I'm relatively new to coding with Python, and I'm currently working on a "PDF-Writer" which should read in PDF files and then draw a certain String to it plus some other stuff. I'm doing this with canvas and PyPDF2 and it works fine.
My question is referring to this question:
(https://stackoverflow.com/questions/1180115/add-text-to-existing-pdf-using-python#comment77571019_17538003)
Others already commented under the post but didn't post any answers there that's why I'm asking this question. I would have commented under the post, but sadly, I don't have the reputation to comment. But I think resolving the issue by asking a new question is also fine.
My problems
- My first problem is that I can only write on the first page of the document. I would like to write on all pages of the document.
- My second problem is that the first page of the document is also the last page for some reason.
Here my Code:
from PyPDF2 import PdfFileWriter, PdfFileReader
import io
from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import letter
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont
from reportlab.lib.units import cm
file = input("Give path to the file >> ")
packet = io.BytesIO()
can = canvas.Canvas(packet, pagesize=letter)
# register font
pdfmetrics.registerFont(TTFont('Arial', 'Arial.ttf'))
can.setFont(psfontname= "Arial",size = 10)
# create a new string
can.drawString(93, 54.5, "2022-12-21")
# create a white rectangle
can.setFillColorRGB(254,254,254)
can.rect(92.5,65,2*cm,0.34*cm, fill=1,stroke=0)
# save the pdf
can.save()
# move to the beginning of the StringIO buffer
packet.seek(0)
# create a new PDF with Reportlab
new_pdf = PdfFileReader(packet)
# read your existing PDF
existing_pdf = PdfFileReader(open(file, "rb"))
output = PdfFileWriter()
# add the "watermark" (which is the new pdf) on the existing page
page = existing_pdf.pages[0]
page.merge_page(new_pdf.pages[0])
output.append_pages_from_reader(existing_pdf)
output.add_page(page)
# finally, write "output" to a real file
output_stream = open("destination3.pdf", "wb")
output.write(output_stream)
output_stream.close()
print("PDF created!")
What I've tried
Well, I added output.append_pages_from_reader(existing_pdf)
to the code which led to the problems. Now I'm searching after a properly working Code
答案1
得分: 0
这段代码实际上做了我在上面问题中期望的事情。
英文:
Well I got the answer, here is an excerpt:
existing_pdf = PdfFileReader(open(filename2, "rb"))
output = PdfFileWriter()
# add the "watermark" (which is the new pdf) on the existing pages
for x in range(existing_pdf.getNumPages()):
page = existing_pdf.pages[x]
page.merge_page(new_pdf.pages[0])
output.add_page(page)
# finally, write "output" to a real file
output_stream = open(outputPath, "wb")
output.write(output_stream)
output_stream.close()
print("PDF created!")
This code does exactly what I was expecting in my question above.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论