Gets text from a PDF file. PDF can be either a URL or a file path on the local file system. @Params pdf - The URL or file path of the PDF file. page - The page number (or a list of page numbers) of the PDF. If a page number is provided, looks only at t
(
self,
pdf,
page=None,
maxpages=None,
password=None,
codec="utf-8",
wrap=False,
nav=False,
override=False,
caching=True,
)
| 7719 | return str(text) |
| 7720 | |
| 7721 | def get_pdf_text( |
| 7722 | self, |
| 7723 | pdf, |
| 7724 | page=None, |
| 7725 | maxpages=None, |
| 7726 | password=None, |
| 7727 | codec="utf-8", |
| 7728 | wrap=False, |
| 7729 | nav=False, |
| 7730 | override=False, |
| 7731 | caching=True, |
| 7732 | ): |
| 7733 | """Gets text from a PDF file. |
| 7734 | PDF can be either a URL or a file path on the local file system. |
| 7735 | @Params |
| 7736 | pdf - The URL or file path of the PDF file. |
| 7737 | page - The page number (or a list of page numbers) of the PDF. |
| 7738 | If a page number is provided, looks only at that page. |
| 7739 | (1 is the first page, 2 is the second page, etc.) |
| 7740 | If no page number is provided, returns all PDF text. |
| 7741 | maxpages - Instead of providing a page number, you can provide |
| 7742 | the number of pages to use from the beginning. |
| 7743 | password - If the PDF is password-protected, enter it here. |
| 7744 | codec - The compression format for character encoding. |
| 7745 | (The default codec used by this method is 'utf-8'.) |
| 7746 | wrap - Replaces ' \n' with ' ' so that individual sentences |
| 7747 | from a PDF don't get broken up into separate lines when |
| 7748 | getting converted into text format. |
| 7749 | nav - If PDF is a URL, navigates to the URL in the browser first. |
| 7750 | (Not needed because the PDF will be downloaded anyway.) |
| 7751 | override - If the PDF file to be downloaded already exists in the |
| 7752 | downloaded_files/ folder, that PDF will be used |
| 7753 | instead of downloading it again. |
| 7754 | caching - If resources should be cached via pdfminer.""" |
| 7755 | import warnings |
| 7756 | |
| 7757 | with warnings.catch_warnings(): |
| 7758 | warnings.simplefilter("ignore", category=UserWarning) |
| 7759 | pip_find_lock = fasteners.InterProcessLock( |
| 7760 | constants.PipInstall.FINDLOCK |
| 7761 | ) |
| 7762 | with pip_find_lock: |
| 7763 | with suppress(Exception): |
| 7764 | shared_utils.make_writable(constants.PipInstall.FINDLOCK) |
| 7765 | try: |
| 7766 | from pdfminer.high_level import extract_text |
| 7767 | except Exception: |
| 7768 | shared_utils.pip_install("pdfminer.six") |
| 7769 | from pdfminer.high_level import extract_text |
| 7770 | if not password: |
| 7771 | password = "" |
| 7772 | if not maxpages: |
| 7773 | maxpages = 0 |
| 7774 | if not pdf.lower().endswith(".pdf"): |
| 7775 | raise Exception("%s is not a PDF file! (Expecting a .pdf)" % pdf) |
| 7776 | file_path = None |
| 7777 | if page_utils.is_valid_url(pdf): |
| 7778 | downloads_folder = download_helper.get_downloads_folder() |