首页 > 解决方案 > 如何在python中使用pyPDF2更改pdf文件中的字体

问题描述

如何更改 pyPDF 2 模块中的字体。

我试过了print(help(canvas.Canvas))

我尝试了initialFontName = NoneinitialFontSize = None

但我的文字没有改变。另外,我正在使用带有 raspbian 操作系统的 raspberry-pi 来执行此操作。

这是我的代码

        can = canvas.Canvas(packet, pagesize=letter, initialFontName = 'Helvetica', initialFontSize = 5)
        now = datetime.datetime.now()

        can.drawString(250, 500, self.get_Firstn1 + ' ' + self.get_Middlen1 +' ' + self.get_Lastn1)
        can.drawString(300, 550, "Hello World")
        can.drawString(350, 600,"Hello")

        can.save()

        packet.seek(0)
        new_pdf = PdfFileReader(packet)


        existing_pdf = PdfFileReader(open("Sample.pdf", "rb"))
        existing_pdf.decrypt('')
        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)

        #write "output" to a real file
        outputStream = open("destination.pdf", "wb")
        output.write(outputStream)
        outputStream.close()
            #open pdf file
        opener ="open" if sys.platform == "darwin"else "xdg-open"
        subprocess.call([opener, "destination.pdf"])

我尝试更改 Arial、Times New Roman、Cambria 等字体,但它只接受 Helvetica 并且文本大小和字体没有改变。

标签: pythonpypdf2

解决方案


推荐阅读