-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #95 from felipealfonsog/development
Development
- Loading branch information
Showing
3 changed files
with
100 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,18 +23,10 @@ | |
* You can install it using pip: pip install PyMuPDF | ||
* Make sure to include the appropriate model or adapt it for your needs. | ||
************************************************* | ||
* How to run the TermPDF Viewer: | ||
* Clone the TermPDF Viewer repository from GitHub. | ||
* Navigate to the project directory: | ||
* cd TermPDF-Viewer | ||
* Install PyMuPDF library (if not already installed): | ||
pip install PyMuPDF | ||
* Run the TermPDF Viewer: | ||
* python termpdf.py | ||
pip install termcolor | ||
* The TermPDF Viewer will start, allowing you to: | ||
* Scan for PDF files in the current directory. | ||
|
@@ -53,37 +45,31 @@ | |
''' | ||
|
||
|
||
|
||
|
||
import os | ||
import fitz | ||
import re | ||
from termcolor import colored | ||
|
||
def scan_pdf_files(): | ||
pdf_files = [file for file in os.listdir('.') if file.lower().endswith('.pdf')] | ||
return pdf_files | ||
|
||
''' | ||
def display_pdf(pdf_filename): | ||
doc = fitz.open(pdf_filename) | ||
total_pages = doc.page_count | ||
current_page = 0 | ||
while True: | ||
page = doc[current_page] | ||
print(f'Page {current_page + 1} / {total_pages}') | ||
print(page.get_text()) | ||
choice = input("Enter 'b' to go back, 'f' to go forward, 'q' to quit: ") | ||
if choice == 'b': | ||
current_page = max(current_page - 1, 0) | ||
elif choice == 'f': | ||
current_page = min(current_page + 1, total_pages - 1) | ||
elif choice == 'q': | ||
break | ||
def display_current_page(doc, current_page, total_pages, keyword=None): | ||
if not (0 <= current_page < total_pages): | ||
print("Invalid page number.") | ||
return | ||
|
||
page = doc[current_page] | ||
print(f'Page {current_page + 1} / {total_pages}') | ||
text = page.get_text() | ||
|
||
if keyword: | ||
# Highlight occurrences of the keyword using regular expressions | ||
highlighted_text = re.sub(rf'(?i)\b{re.escape(keyword)}\b', lambda match: colored(match.group(), 'red', attrs=['bold']), text) | ||
print(highlighted_text) | ||
else: | ||
print(text) | ||
|
||
doc.close() | ||
''' | ||
|
||
def display_pdf(pdf_filename): | ||
try: | ||
doc = fitz.open(pdf_filename) | ||
|
@@ -96,53 +82,100 @@ def display_pdf(pdf_filename): | |
break | ||
|
||
page = doc[current_page] | ||
print(f'Page {current_page + 1} / {total_pages}') | ||
print(page.get_text()) | ||
display_current_page(doc, current_page, total_pages) | ||
|
||
choice = input("Enter 'b' to go back, 'f' to go forward, 'q' to quit: ") | ||
choice = input("Enter 'b' to go back, 'f' to go forward, 's' to search, 'q' to quit: ") | ||
if choice == 'b': | ||
current_page = max(current_page - 1, 0) | ||
elif choice == 'f': | ||
current_page = min(current_page + 1, total_pages - 1) | ||
elif choice == 'q': | ||
break | ||
elif choice.lower() == 's': | ||
keyword = input("Enter the keyword to search: ").lower() | ||
found = False | ||
|
||
for page_num in range(current_page, total_pages): | ||
text = doc[page_num].get_text().lower() | ||
if keyword in text: | ||
display_current_page(doc, page_num, total_pages, keyword) | ||
found = True | ||
break | ||
|
||
if not found: | ||
print(f"No matches found for '{keyword}'.") | ||
else: | ||
while True: | ||
response = input("Press Enter to continue searching or press Esc to exit search: ") | ||
if response.lower() == '': | ||
break | ||
elif response.lower() == 'esc': | ||
return | ||
|
||
except Exception as e: | ||
print(f"Error: {e}") | ||
finally: | ||
if 'doc' in locals(): | ||
doc.close() | ||
|
||
|
||
|
||
|
||
def main(): | ||
|
||
print("\n") | ||
print("▀▀█▀▀ █▀▀ █▀▀█ █▀▄▀█ ▒█▀▀█ ▒█▀▀▄ ▒█▀▀▀ ▒█░░▒█ ░▀░ █▀▀ █░░░█ █▀▀ █▀▀█") | ||
print("░▒█░░ █▀▀ █▄▄▀ █░▀░█ ▒█▄▄█ ▒█░▒█ ▒█▀▀▀ ░▒█▒█░ ▀█▀ █▀▀ █▄█▄█ █▀▀ █▄▄▀") | ||
print("░▒█░░ ▀▀▀ ▀░▀▀ ▀░░░▀ ▒█░░░ ▒█▄▄▀ ▒█░░░ ░░▀▄▀░ ▀▀▀ ▀▀▀ ░▀░▀░ ▀▀▀ ▀░▀▀") | ||
print("-------------------------------------------------------------------------") | ||
print("TermPDF Viewer - view and navigate PDF files within the terminal.") | ||
print("-------------------------------------------------------------------------") | ||
print("* Simple: TermPDF Viewer is a Python program that enables users to ") | ||
print("* view and navigate PDF files directly within the terminal.") | ||
print("-------------------------------------------------------------------------") | ||
print("\n\n" | ||
print("\n" | ||
"▀▀█▀▀ █▀▀ █▀▀█ █▀▄▀█ ▒█▀▀█ ▒█▀▀▄ ▒█▀▀▀ ▒█░░▒█ ░▀░ █▀▀ █░░░█ █▀▀ █▀▀█\n" | ||
"░▒█░░ █▀▀ █▄▄▀ █░▀░█ ▒█▄▄█ ▒█░▒█ ▒█▀▀▀ ░▒█▒█░ ▀█▀ █▀▀ █▄█▄█ █▀▀ █▄▄▀\n" | ||
"░▒█░░ ▀▀▀ ▀░▀▀ ▀░░░▀ ▒█░░░ ▒█▄▄▀ ▒█░░░ ░░▀▄▀░ ▀▀▀ ▀▀▀ ░▀░▀░ ▀▀▀ ▀░▀▀\n" | ||
"-------------------------------------------------------------------------\n" | ||
"TermPDF Viewer - view and navigate PDF files within the terminal.\n" | ||
"-------------------------------------------------------------------------\n" | ||
"* Simple: TermPDF Viewer is a Python program that enables users to \n" | ||
"* view and navigate PDF files directly within the terminal.\n" | ||
"-------------------------------------------------------------------------\n" | ||
"╭────────────-----────── TERMS OF USE ──────────----------───╮\n" | ||
"│ This software is licensed under the MIT License. │\n" | ||
"│ By Felipe Alfonso González - github.com/felipealfonsog │\n" | ||
"│ Computer Science Engineer - Email: [email protected] │\n" | ||
"╰───────────────────────────────────────────────---------────╯\n" | ||
"\n") | ||
"-------------------------------------------------------------------------\n" | ||
"* Prerequisites:\n" | ||
"* Python 3.x: The program is written in Python and requires a Python 3.x interpreter to run.\n" | ||
"* PyMuPDF: A Python binding for the MuPDF library, used to handle PDF file rendering and interaction.\n" | ||
"* You can install it using pip: pip install PyMuPDF\n" | ||
"* Make sure to include the appropriate model or adapt it for your needs.\n" | ||
"-------------------------------------------------------------------------\n" | ||
"* Important Notes:\n" | ||
"\n" | ||
"* Install PyMuPDF library (if not already installed):\n" | ||
" pip install PyMuPDF\n" | ||
" pip install termcolor\n" | ||
"\n" | ||
"* The TermPDF Viewer will start, allowing you to: \n" | ||
"* Scan for PDF files in the current directory.\n" | ||
"* Select a PDF file to view by entering its number.\n" | ||
"* View the PDF with options to move back, forward, search, or return to the main menu.\n" | ||
"* Quit and return to the main menu.\n" | ||
"* To exit the TermPDF Viewer, use 'q' in the main menu.\n" | ||
"* To search within the PDF, use 's' during viewing and enter the keyword to search.\n" | ||
"-------------------------------------------------------------------------\n" | ||
"* Important Notes:\n" | ||
"* - The application has been tested on Linux and macOS.\n" | ||
"* - For Windows, additional configurations may be required.\n" | ||
"* - Make sure to fulfill the prerequisites before running the application.\n" | ||
"* - For more information, please refer to the project documentation.\n" | ||
"-------------------------------------------------------------------------\n" | ||
) | ||
|
||
print("Welcome to the TermPDF Viewer!") | ||
|
||
while True: | ||
print("\nMain Menu:") | ||
print("1. Scan for PDF files") | ||
print("2. View scanned PDF files") | ||
print("3. Quit") | ||
|
||
choice = input("Enter the number of your choice: ") | ||
|
||
if choice == '1': | ||
pdf_files = scan_pdf_files() | ||
if not pdf_files: | ||
|
@@ -151,7 +184,7 @@ def main(): | |
print("Scanned PDF files:") | ||
for i, pdf_file in enumerate(pdf_files, start=1): | ||
print(f"{i}. {pdf_file}") | ||
|
||
elif choice == '2': | ||
pdf_files = scan_pdf_files() | ||
if not pdf_files: | ||
|
@@ -160,7 +193,7 @@ def main(): | |
print("Scanned PDF files:") | ||
for i, pdf_file in enumerate(pdf_files, start=1): | ||
print(f"{i}. {pdf_file}") | ||
|
||
file_choice = input("Enter the number of the PDF file to view (or 'q' to go back): ") | ||
if file_choice == 'q': | ||
continue | ||
|
@@ -172,13 +205,15 @@ def main(): | |
print("Invalid choice.") | ||
except ValueError: | ||
print("Invalid input. Please enter a valid number.") | ||
|
||
elif choice == '3': | ||
print("Goodbye!") | ||
break | ||
|
||
else: | ||
print("Invalid choice. Please enter a valid number.") | ||
|
||
if __name__ == '__main__': | ||
main() | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters