首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >pypdf python工具

pypdf python工具
EN

Stack Overflow用户
提问于 2010-10-04 19:54:54
回答 2查看 2.7K关注 0票数 2

如何使用pypdf python模块读取以下pdf文件http://www.envis-icpe.com/pointcounterpointbook/Hindi_Book.pdf

代码语言:javascript
复制
# -*- coding: utf-8 -*-
from pyPdf import PdfFileWriter, PdfFileReader
import pyPdf

def getPDFContent(path):
   content = ""
   # Load PDF into pyPDF
   pdf = pyPdf.PdfFileReader(file(path, "rb"))
   # Iterate pages
   for i in range(0, pdf.getNumPages()):
      # Extract text from page and add to content
      content += pdf.getPage(i).extractText() + "\n"
   # Collapse whitespace
   content = " ".join(content.replace(u"\xa0", " ").strip().split())
   return content

print getPDFContent("/home/tom/Desktop/Hindi_Book.pdf").encode("ascii", "xmlcharrefreplace")

上面的命令仅打印二进制文件

以及如何打印以下代码中的内容

代码语言:javascript
复制
from pyPdf import PdfFileWriter, PdfFileReader
import sys
import pyPdf

from pyPdf import PdfFileWriter, PdfFileReader

output = PdfFileWriter()
input1 = PdfFileReader(file("/home/tom/Desktop/Hindi_Book.pdf", "rb"))

# print the title of document1.pdf
print "title = %s" % (input1.getDocumentInfo().title)
EN

回答 2

Stack Overflow用户

发布于 2010-10-04 22:07:21

请注意,您引用的pdf文档的大多数“文本”根本不是真正的文本:它主要是图像。当我尝试时,实际的文本似乎被正确地提取出来(尽管我必须承认,除了首页上的一些片段和页码,我无法阅读它;-))。

至于第二个问题:我不确定你在问什么。

票数 1
EN

Stack Overflow用户

发布于 2017-08-20 19:22:42

如果要从pdf文件中编写特定文本,可以使用exctractText(),如下所示:

代码语言:javascript
复制
from path_to_folder import main_path as my_text
import os
from PyPDF2 import PdfFileReader

my_pdf_path = os.path.join(my_text, "my_pdf.pdf")

with open(os.path.join(my_text, "out_put.txt"), 'w') as out_text:
    pdf_read = PdfFileReader(open(my_pdf_path, "rb"))
    out_text.write(pdf_read.getDocumentInfo().title)
    for pages in range(pdf_read.getNumPages()):
        text = pdf_read.getPage(pages).extractText()
        out_text.write(text)

在上面的示例中,我只是从每个页面提取文本并将其写入文本文件。你可以选择任何东西。如果您需要将特定页面作为pdf,您可以使用以下代码:

代码语言:javascript
复制
from pyPdf import PdfFileWriter, PdfFileReader
import os, sys
main_path = "/home/tom/Desktop/"
output_file = PdfFileWriter()
input_file = PdfFileReader(file("/home/tom/Desktop/Hindi_Book.pdf", "rb"))
for page_number in range(input_file.getNumPages()):
    output_file.addPage(input_file.getPage(page_number))

new_file = os.path.join(main_path, "Out_folder/new_pdf.pdf")
out_fil1 = open(new_file, "wb")
output_file.write(out_fil1)
output_file.close()

你提供的链接不起作用,这就是为什么我看不到文件的原因。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3854963

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档