V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
lanceadd
V2EX  ›  Python

Reportlab 如何向已经存在 pdf 中的指定位置插入一个饼状图

  •  1
     
  •   lanceadd · 2020-11-13 10:58:54 +08:00 · 1437 次点击
    这是一个创建于 1469 天前的主题,其中的信息可能已经有所发展或是发生改变。

    我尝试使用 PyPDF2 和 Reportlab 向一个已经存在的 pdf 中的指定位置插入一个由 reportlab 生成的饼状图,但是我看的时候只看到使用 reportlab 的 canvas 向其中写入字符串,图片啥的,没看到写入一个 pie 对象,是要将这个 pie 对象转成图片然后再插入吗,我参考的是这个代码,先感谢各位大佬的教育

    from PyPDF2 import PdfFileWriter, PdfFileReader
    import io
    from reportlab.pdfgen import canvas
    from reportlab.lib.pagesizes import letter
    
    packet = io.BytesIO()
    # create a new PDF with Reportlab
    can = canvas.Canvas(packet, pagesize=letter)
    can.drawString(10, 100, "Hello world")
    can.save()
    
    #move to the beginning of the StringIO buffer
    packet.seek(0)
    new_pdf = PdfFileReader(packet)
    # read your existing PDF
    existing_pdf = PdfFileReader(open("original.pdf", "rb"))
    output = PdfFileWriter()
    # add the "watermark" (which is the new pdf) on the existing page
    page = existing_pdf.getPage(0)
    page.mergePage(new_pdf.getPage(0))
    output.addPage(page)
    # finally, write "output" to a real file
    outputStream = open("destination.pdf", "wb")
    output.write(outputStream)
    outputStream.close()
    
    2 条回复    2020-11-13 13:19:33 +08:00
    lanceadd
        1
    lanceadd  
    OP
       2020-11-13 11:55:45 +08:00
    我会了
    ```
    import io

    from PyPDF2 import PdfFileReader, PdfFileWriter
    from reportlab.graphics import renderPDF
    from reportlab.graphics.charts.piecharts import Pie
    from reportlab.graphics.shapes import Drawing
    from reportlab.lib.pagesizes import A4
    from reportlab.pdfgen import canvas

    packet = io.BytesIO()

    can = canvas.Canvas(packet, pagesize=A4)

    pie = Pie()
    pie.data = [20, 10, 5, 5, 5]
    pie.labels = ['a', 'b', 'c', 'd', 'e']
    pie.sideLabels = True

    d = Drawing(100, 100)
    d.add(pie)
    renderPDF.draw(d, can, 20, 20)
    can.save()
    packet.seek(0)
    new_pdf = PdfFileReader(packet)

    existing_pdf = PdfFileReader(open('example_06.pdf', 'rb'))
    output = PdfFileWriter()
    page = existing_pdf.getPage(0)
    page.mergePage(new_pdf.getPage(0))
    output.addPage(page)
    outputStream = open('result.pdf', 'wb')
    output.write(outputStream)
    outputStream.close()

    ```
    renmu123
        2
    renmu123  
       2020-11-13 13:19:33 +08:00 via Android
    提示一下,pypdf2 已经停止维护了,可以试试 pypdf4 (虽然也快了
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1014 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 22ms · UTC 19:47 · PVG 03:47 · LAX 11:47 · JFK 14:47
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.