要将 pytesseract 输出为 PDF 文件,需要使用 Python 中的 PyPDF2 库。以下是一个示例代码:

import pytesseract
from PIL import Image
import io
import os
from PyPDF2 import PdfFileWriter, PdfFileReader

# 读取图像并提取文本
image_path = 'image.jpg'
text = pytesseract.image_to_string(Image.open(image_path))

# 创建 PDF 文件
output_pdf = PdfFileWriter()

# 将文本添加到 PDF 文件
output_pdf.addPage(PdfFileReader(io.BytesIO(text.encode('utf-8'))).getPage(0))

# 将 PDF 文件保存到磁盘
output_filename = 'out.pdf'
with open(output_filename, 'wb') as f:
    output_pdf.write(f)

# 打开 PDF 文件
os.startfile(output_filename)

在这个示例代码中,我们首先使用 pytesseract 库从图像中提取文本。然后,我们使用 PyPDF2 库创建一个新的 PDF 文件,并将文本添加到该文件中。最后,我们将 PDF 文件保存到磁盘,并使用操作系统的默认程序打开该文件。

pytesseract 输出为outpdf

原文地址: https://www.cveoy.top/t/topic/bpaa 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录