high_level.py 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. """Functions that can be used for the most common use-cases for pdfminer.six"""
  2. import logging
  3. import sys
  4. from io import StringIO
  5. from typing import Any, BinaryIO, Container, Iterator, Optional, cast
  6. from pdfminer.converter import (
  7. HOCRConverter,
  8. HTMLConverter,
  9. PDFPageAggregator,
  10. TextConverter,
  11. XMLConverter,
  12. )
  13. from pdfminer.image import ImageWriter
  14. from pdfminer.layout import LAParams, LTPage
  15. from pdfminer.pdfdevice import PDFDevice, TagExtractor
  16. from pdfminer.pdfexceptions import PDFValueError
  17. from pdfminer.pdfinterp import PDFPageInterpreter, PDFResourceManager
  18. from pdfminer.pdfpage import PDFPage
  19. from pdfminer.utils import AnyIO, FileOrName, open_filename
  20. def extract_text_to_fp(
  21. inf: BinaryIO,
  22. outfp: AnyIO,
  23. output_type: str = "text",
  24. codec: str = "utf-8",
  25. laparams: Optional[LAParams] = None,
  26. maxpages: int = 0,
  27. page_numbers: Optional[Container[int]] = None,
  28. password: str = "",
  29. scale: float = 1.0,
  30. rotation: int = 0,
  31. layoutmode: str = "normal",
  32. output_dir: Optional[str] = None,
  33. strip_control: bool = False,
  34. debug: bool = False,
  35. disable_caching: bool = False,
  36. **kwargs: Any,
  37. ) -> None:
  38. """Parses text from inf-file and writes to outfp file-like object.
  39. Takes loads of optional arguments but the defaults are somewhat sane.
  40. Beware laparams: Including an empty LAParams is not the same as passing
  41. None!
  42. :param inf: a file-like object to read PDF structure from, such as a
  43. file handler (using the builtin `open()` function) or a `BytesIO`.
  44. :param outfp: a file-like object to write the text to.
  45. :param output_type: May be 'text', 'xml', 'html', 'hocr', 'tag'.
  46. Only 'text' works properly.
  47. :param codec: Text decoding codec
  48. :param laparams: An LAParams object from pdfminer.layout. Default is None
  49. but may not layout correctly.
  50. :param maxpages: How many pages to stop parsing after
  51. :param page_numbers: zero-indexed page numbers to operate on.
  52. :param password: For encrypted PDFs, the password to decrypt.
  53. :param scale: Scale factor
  54. :param rotation: Rotation factor
  55. :param layoutmode: Default is 'normal', see
  56. pdfminer.converter.HTMLConverter
  57. :param output_dir: If given, creates an ImageWriter for extracted images.
  58. :param strip_control: Does what it says on the tin
  59. :param debug: Output more logging data
  60. :param disable_caching: Does what it says on the tin
  61. :param other:
  62. :return: nothing, acting as it does on two streams. Use StringIO to get
  63. strings.
  64. """
  65. if debug:
  66. logging.getLogger().setLevel(logging.DEBUG)
  67. imagewriter = None
  68. if output_dir:
  69. imagewriter = ImageWriter(output_dir)
  70. rsrcmgr = PDFResourceManager(caching=not disable_caching)
  71. device: Optional[PDFDevice] = None
  72. if output_type != "text" and outfp == sys.stdout:
  73. outfp = sys.stdout.buffer
  74. if output_type == "text":
  75. device = TextConverter(
  76. rsrcmgr,
  77. outfp,
  78. codec=codec,
  79. laparams=laparams,
  80. imagewriter=imagewriter,
  81. )
  82. elif output_type == "xml":
  83. device = XMLConverter(
  84. rsrcmgr,
  85. outfp,
  86. codec=codec,
  87. laparams=laparams,
  88. imagewriter=imagewriter,
  89. stripcontrol=strip_control,
  90. )
  91. elif output_type == "html":
  92. device = HTMLConverter(
  93. rsrcmgr,
  94. outfp,
  95. codec=codec,
  96. scale=scale,
  97. layoutmode=layoutmode,
  98. laparams=laparams,
  99. imagewriter=imagewriter,
  100. )
  101. elif output_type == "hocr":
  102. device = HOCRConverter(
  103. rsrcmgr,
  104. outfp,
  105. codec=codec,
  106. laparams=laparams,
  107. stripcontrol=strip_control,
  108. )
  109. elif output_type == "tag":
  110. # Binary I/O is required, but we have no good way to test it here.
  111. device = TagExtractor(rsrcmgr, cast(BinaryIO, outfp), codec=codec)
  112. else:
  113. msg = f"Output type can be text, html, xml or tag but is {output_type}"
  114. raise PDFValueError(msg)
  115. assert device is not None
  116. interpreter = PDFPageInterpreter(rsrcmgr, device)
  117. for page in PDFPage.get_pages(
  118. inf,
  119. page_numbers,
  120. maxpages=maxpages,
  121. password=password,
  122. caching=not disable_caching,
  123. ):
  124. page.rotate = (page.rotate + rotation) % 360
  125. interpreter.process_page(page)
  126. device.close()
  127. def extract_text(
  128. pdf_file: FileOrName,
  129. password: str = "",
  130. page_numbers: Optional[Container[int]] = None,
  131. maxpages: int = 0,
  132. caching: bool = True,
  133. codec: str = "utf-8",
  134. laparams: Optional[LAParams] = None,
  135. ) -> str:
  136. """Parse and return the text contained in a PDF file.
  137. :param pdf_file: Either a file path or a file-like object for the PDF file
  138. to be worked on.
  139. :param password: For encrypted PDFs, the password to decrypt.
  140. :param page_numbers: List of zero-indexed page numbers to extract.
  141. :param maxpages: The maximum number of pages to parse
  142. :param caching: If resources should be cached
  143. :param codec: Text decoding codec
  144. :param laparams: An LAParams object from pdfminer.layout. If None, uses
  145. some default settings that often work well.
  146. :return: a string containing all of the text extracted.
  147. """
  148. if laparams is None:
  149. laparams = LAParams()
  150. with open_filename(pdf_file, "rb") as fp, StringIO() as output_string:
  151. fp = cast(BinaryIO, fp) # we opened in binary mode
  152. rsrcmgr = PDFResourceManager(caching=caching)
  153. device = TextConverter(rsrcmgr, output_string, codec=codec, laparams=laparams)
  154. interpreter = PDFPageInterpreter(rsrcmgr, device)
  155. for page in PDFPage.get_pages(
  156. fp,
  157. page_numbers,
  158. maxpages=maxpages,
  159. password=password,
  160. caching=caching,
  161. ):
  162. interpreter.process_page(page)
  163. return output_string.getvalue()
  164. def extract_pages(
  165. pdf_file: FileOrName,
  166. password: str = "",
  167. page_numbers: Optional[Container[int]] = None,
  168. maxpages: int = 0,
  169. caching: bool = True,
  170. laparams: Optional[LAParams] = None,
  171. ) -> Iterator[LTPage]:
  172. """Extract and yield LTPage objects
  173. :param pdf_file: Either a file path or a file-like object for the PDF file
  174. to be worked on.
  175. :param password: For encrypted PDFs, the password to decrypt.
  176. :param page_numbers: List of zero-indexed page numbers to extract.
  177. :param maxpages: The maximum number of pages to parse
  178. :param caching: If resources should be cached
  179. :param laparams: An LAParams object from pdfminer.layout. If None, uses
  180. some default settings that often work well.
  181. :return: LTPage objects
  182. """
  183. if laparams is None:
  184. laparams = LAParams()
  185. with open_filename(pdf_file, "rb") as fp:
  186. fp = cast(BinaryIO, fp) # we opened in binary mode
  187. resource_manager = PDFResourceManager(caching=caching)
  188. device = PDFPageAggregator(resource_manager, laparams=laparams)
  189. interpreter = PDFPageInterpreter(resource_manager, device)
  190. for page in PDFPage.get_pages(
  191. fp,
  192. page_numbers,
  193. maxpages=maxpages,
  194. password=password,
  195. caching=caching,
  196. ):
  197. interpreter.process_page(page)
  198. layout = device.get_result()
  199. yield layout