Created
June 18, 2025 14:42
-
-
Save pmacMaps/0801368af15bb0db00b0fb61669e4d24 to your computer and use it in GitHub Desktop.
Convert a PDF file into an MP3 file
This file contains hidden or 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
# Note: logic in app created via Vibe Coding using ChatGPT. You can review conversation at https://chatgpt.com/share/6852cff0-13a0-800b-8b0d-96a655a387cb | |
# This script converts a pdf of text into a MP3 file. Can be useful to converting long reports into audio files. | |
from gtts import gTTS | |
from PyPDF2 import PdfReader | |
from pydub import AudioSegment | |
import os | |
import sys | |
# === PDF TEXT EXTRACTION === | |
def extract_text(pdf_file): | |
reader = PdfReader(pdf_file) | |
text = '' | |
for page in reader.pages: | |
page_text = page.extract_text() | |
if page_text: | |
text += page_text + '\n' | |
return text | |
# === TEXT SPLITTING === | |
def split_text(text, max_length): | |
chunks = [] | |
while len(text) > max_length: | |
split_at = text.rfind('.', 0, max_length) # Try to split at sentence end | |
if split_at == -1: | |
split_at = max_length | |
chunks.append(text[:split_at+1].strip()) | |
text = text[split_at+1:].strip() | |
if text: | |
chunks.append(text) | |
return chunks | |
# === CONVERT CHUNKS TO MP3 === | |
def convert_chunks_to_audio(chunks, output_folder): | |
os.makedirs(output_folder, exist_ok=True) | |
for i, chunk in enumerate(chunks): | |
tts = gTTS(text=chunk, lang=lang, tld=tld) | |
chunk_path = os.path.join(output_folder, f'part_{i:03d}.mp3') | |
tts.save(chunk_path) | |
print(f"Saved: {chunk_path}") | |
# === MERGE MP3 CHUNKS === | |
def merge_audio_files(input_folder, output_file): | |
audio_files = sorted([ | |
f for f in os.listdir(input_folder) if f.endswith('.mp3') | |
]) | |
combined = AudioSegment.empty() | |
for file in audio_files: | |
audio = AudioSegment.from_mp3(os.path.join(input_folder, file)) | |
combined += audio | |
combined.export(output_file, format='mp3') | |
print(f"\n✅ Final audio saved as: {output_file}") | |
# === MAIN === | |
if __name__ == '__main__': | |
try: | |
# === CONFIG === | |
pdf_path = r'Path\To\File\file.pdf' # Input PDF | |
output_dir = 'audio_chunks' # Temp folder for MP3 chunks | |
final_output = 'Output_File.mp3' # Final combined MP3 | |
chunk_size = 4500 # Max characters per chunk (gTTS limit) | |
lang = 'en' # Language | |
tld = 'com' # American accent | |
print("Extracting text...") | |
full_text = extract_text(pdf_path) | |
print("Completed extracting text.") | |
print("Splitting text...") | |
chunks = split_text(full_text, chunk_size) | |
print("Completed splitting text.") | |
print(f"Converting {len(chunks)} chunks to audio...") | |
convert_chunks_to_audio(chunks, output_dir) | |
print("Completed converting chunks to audio.") | |
print("Merging audio chunks...") | |
merge_audio_files(output_dir, final_output) | |
print("Completed merging audio chunks.") | |
except Exception as e: | |
tbE = sys.exc_info()[2] | |
# Write the line number the error occured to the log file | |
print(f'error at Line {tbE.tb_lineno}') | |
# Write the error print( to the log file | |
print(f'error: {e}') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment