-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpdf-to-text.py
More file actions
35 lines (26 loc) · 905 Bytes
/
Copy pathpdf-to-text.py
File metadata and controls
35 lines (26 loc) · 905 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import argparse
import pdftotext
def main(pdf_file, password=None):
# Load your PDF
with open(pdf_file, "rb") as f:
if password:
pdf = pdftotext.PDF(f, password)
else:
pdf = pdftotext.PDF(f)
# How many pages?
print(f"Number of pages: {len(pdf)}")
# Iterate over all the pages
for i, page in enumerate(pdf):
print(f"\n--- Page {i + 1} ---")
print(page)
# Read all the text into one string
print("\n\n--- All Text ---\n")
print("\n\n".join(pdf))
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Extract text from a PDF file.")
parser.add_argument("pdf_file", help="Path to the PDF file")
parser.add_argument(
"--password", help="Password for password-protected PDF files", default=None
)
args = parser.parse_args()
main(args.pdf_file, args.password)