pdf-speak/pdf-speak.py

30 lines
1.1 KiB
Python

from pypdf import PdfReader
import subprocess
import argparse
def tts(text):
piper_model_json_path = "piper-models/ihfq9facjxhl8b8z3afhn1kisq5wsghg-hfc_female-medium-en_US-hfc_female-medium.onnx.json"
piper_model_path = "piper-models/dy5s1ri7ixy1c27fg4adaf8ji3hmqiic-hfc_female-medium-en_US-hfc_female-medium.onnx"
command = f"echo \"{text}. \" | piper -q -m {piper_model_path} -c {piper_model_json_path} --output-raw | aplay -q -r 22050 -f S16_LE -t raw -"
process = subprocess.run(command, shell=True, check=True)
def read_pdf(pdf_name, page_num):
reader = PdfReader(pdf_name)
page = reader.pages[page_num-1]
text = page.extract_text().replace("\n","").split(". ")
for sentence in text:
print(sentence)
tts(sentence)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Read PDF and convert to speech')
parser.add_argument("-f", '--pdf-file', type=str, help='Path to the PDF file')
parser.add_argument("-p", '--page', type=int, default=1, help='Page number to read from the PDF')
args = parser.parse_args()
pdf = args.pdf_file
page = args.page
read_pdf(pdf, page)