Source code for tika.pdf

# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License.  You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

from io import StringIO

from bs4 import BeautifulSoup

from tika import parser


[docs] def text_from_pdf_pages(filename): pages_txt = [] # Read PDF file data = parser.from_file(filename, xmlContent=True) xhtml_data = BeautifulSoup(data['content'], features="html.parser") for i, content in enumerate(xhtml_data.find_all('div', attrs={'class': 'page'})): # Parse PDF data using TIKA (xml/html) # It's faster and safer to create a new buffer than truncating it # https://stackoverflow.com/questions/4330812/how-do-i-clear-a-stringio-object _buffer = StringIO() _buffer.write(str(content)) parsed_content = parser.from_buffer(_buffer.getvalue()) # Add pages text = parsed_content['content'].strip() pages_txt.append(text) return pages_txt