Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor PDF to MP3 script: #10

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added book.pdf
Binary file not shown.
33 changes: 24 additions & 9 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,30 @@
import pyttsx3,PyPDF2
import pyttsx3
from PyPDF2 import PdfReader
import re

#insert name of your pdf
pdfreader = PyPDF2.PdfFileReader(open('book.pdf', 'rb'))
def clean_text(text):
# Remove newlines and excessive spaces
text = re.sub(r'\s+', ' ', text)
# Remove any extra spaces around punctuation
text = re.sub(r'\s([?.!,;"](?:\s|$))', r'\1', text)
return text

# Use the correct class PdfReader instead of the deprecated PdfFileReader
pdfreader = PdfReader('book.pdf')
speaker = pyttsx3.init()

for page_num in range(pdfreader.numPages):
text = pdfreader.getPage(page_num).extractText()
clean_text = text.strip().replace('\n', ' ')
print(clean_text)
#name mp3 file whatever you would like
speaker.save_to_file(clean_text, 'story.mp3')
full_text = ""

for page_num in range(len(pdfreader.pages)):
text = pdfreader.pages[page_num].extract_text()
if text: # Check if text is not None
clean_text = clean_text(text)
full_text += clean_text + " "
print(clean_text)

# Save the cleaned text to an MP3 file
speaker.save_to_file(full_text, 'story.mp3')
speaker.runAndWait()

speaker.stop()