小编BSF*_*SFU的帖子

如何用python在pdf中绘制形状?

我想在pdf中绘制一个形状,例如矩形。我尝试过下面的代码,但它在 pdf 中添加文本。我怎样才能画它?

# Add text to Existing PDF using Python

from pyPdf import PdfFileWriter, PdfFileReader
import StringIO
from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import letter

packet = StringIO.StringIO()
# create a new PDF with Reportlab
can = canvas.Canvas(packet, pagesize=letter)
TXT="Sample Text"
can.drawString(300, 70, TXT) #coordinates (x,y)
can.save()

#move to the beginning of the StringIO buffer
packet.seek(0)
new_pdf = PdfFileReader(packet)
# read your existing PDF
existing_pdf = PdfFileReader(file("original.pdf", "rb"))
output = PdfFileWriter()
# add the "watermark" (which is the new …
Run Code Online (Sandbox Code Playgroud)

pdf shapes pypdf python-2.7

2
推荐指数
1
解决办法
6899
查看次数

标签 统计

pdf ×1

pypdf ×1

python-2.7 ×1

shapes ×1