All posts by steve@usefulscripts.co

ai-generated, robot, future

Simple Text To Speech Converter

This is a really simple but effective way to convert text files to mp3 audio files.

Usage

You can use a script like this to convert a text document – like a book – into a spoken audio file. The intonation isn’t quite like a real human, but even so, it’s pretty good and it costs nothing and takes moments to do!

Prerequisites

You will need to install the “edge TTS” and “pydub” libraries.

pip install edge-tts

pip install pydub

How to use it

  1. Save the script below in the folder of your choice
  2. Rename the file you want to convert to audio as “test.txt” and save it in the same folder as the script
  3. Run the script
  4. The audio file is saved as “output.mp3”
# Install  using "pip install edge-tts" and "pip install pydub"


import asyncio
import edge_tts

async def main():
    # Read text
    with open('test.txt', 'rb') as f:
        data = f.read()

    text = data.decode('latin-1')

    voice = "en-GB-LibbyNeural"
    
    rate = +50
    outputFile = "output.mp3"

    communicate = edge_tts.Communicate(text, voice,)
    await communicate.save(outputFile)

loop = asyncio.new_event_loop()
loop.run_until_complete(main())
loop.close()

print("Text saved to audio file")

There are a few settings you can adjust:

1) The voice: this script uses “Libby”, a UK accented female voice. At the end of this post I provide the complete list of Edge TTS voices: just replace “en-GB-LibbyNeural” with your preferred voice
2) Reading speed: you can adjust this using the “rate=” line. In this case, the speed is 50% faster than nominal. Try different settings and see how the reading speed changes.

Edge TTS Voices

How to read the voice labels:
EXAMPLE: en-IN-NeerjaNeural, Gender: Female
en – means the voice speaks English
IN – means the accent is Indian

Name: af-ZA-AdriNeural, Gender: FemaleName: af-ZA-WillemNeural, Gender: Male
Name: am-ET-AmehaNeural, Gender: MaleName: am-ET-MekdesNeural, Gender: Female
Name: ar-AE-FatimaNeural, Gender: FemaleName: ar-AE-HamdanNeural, Gender: Male
Name: ar-BH-AliNeural, Gender: MaleName: ar-BH-LailaNeural, Gender: Female
Name: ar-DZ-AminaNeural, Gender: FemaleName: ar-DZ-IsmaelNeural, Gender: Male
Name: ar-EG-SalmaNeural, Gender: FemaleName: ar-EG-ShakirNeural, Gender: Male
Name: ar-IQ-BasselNeural, Gender: MaleName: ar-IQ-RanaNeural, Gender: Female
Name: ar-JO-SanaNeural, Gender: FemaleName: ar-JO-TaimNeural, Gender: Male
Name: ar-KW-FahedNeural, Gender: MaleName: ar-KW-NouraNeural, Gender: Female
Name: ar-LB-LaylaNeural, Gender: FemaleName: ar-LB-RamiNeural, Gender: Male
Name: ar-LY-ImanNeural, Gender: FemaleName: ar-LY-OmarNeural, Gender: Male
Name: ar-MA-JamalNeural, Gender: MaleName: ar-MA-MounaNeural, Gender: Female
Name: ar-OM-AbdullahNeural, Gender: MaleName: ar-OM-AyshaNeural, Gender: Female
Name: ar-QA-AmalNeural, Gender: FemaleName: ar-QA-MoazNeural, Gender: Male
Name: ar-SA-HamedNeural, Gender: MaleName: ar-SA-ZariyahNeural, Gender: Female
Name: ar-SY-AmanyNeural, Gender: FemaleName: ar-SY-LaithNeural, Gender: Male
Name: ar-TN-HediNeural, Gender: MaleName: ar-TN-ReemNeural, Gender: Female
Name: ar-YE-MaryamNeural, Gender: FemaleName: ar-YE-SalehNeural, Gender: Male
Name: az-AZ-BabekNeural, Gender: MaleName: az-AZ-BanuNeural, Gender: Female
Name: bg-BG-BorislavNeural, Gender: MaleName: bg-BG-KalinaNeural, Gender: Female
Name: bn-BD-NabanitaNeural, Gender: FemaleName: bn-BD-PradeepNeural, Gender: Male
Name: bn-IN-BashkarNeural, Gender: MaleName: bn-IN-TanishaaNeural, Gender: Female
Name: bs-BA-GoranNeural, Gender: MaleName: bs-BA-VesnaNeural, Gender: Female
Name: ca-ES-EnricNeural, Gender: MaleName: ca-ES-JoanaNeural, Gender: Female
Name: cs-CZ-AntoninNeural, Gender: MaleName: cs-CZ-VlastaNeural, Gender: Female
Name: cy-GB-AledNeural, Gender: MaleName: cy-GB-NiaNeural, Gender: Female
Name: da-DK-ChristelNeural, Gender: FemaleName: da-DK-JeppeNeural, Gender: Male
Name: de-AT-IngridNeural, Gender: FemaleName: de-AT-JonasNeural, Gender: Male
Name: de-CH-JanNeural, Gender: MaleName: de-CH-LeniNeural, Gender: Female
Name: de-DE-AmalaNeural, Gender: FemaleName: de-DE-ConradNeural, Gender: Male
Name: de-DE-KatjaNeural, Gender: FemaleName: de-DE-KillianNeural, Gender: Male
Name: el-GR-AthinaNeural, Gender: FemaleName: el-GR-NestorasNeural, Gender: Male
Name: en-AU-NatashaNeural, Gender: FemaleName: en-AU-WilliamNeural, Gender: Male
Name: en-CA-ClaraNeural, Gender: FemaleName: en-CA-LiamNeural, Gender: Male
Name: en-GB-LibbyNeural, Gender: FemaleName: en-GB-MaisieNeural, Gender: Female
Name: en-GB-RyanNeural, Gender: MaleName: en-GB-SoniaNeural, Gender: Female
Name: en-GB-ThomasNeural, Gender: MaleName: en-HK-SamNeural, Gender: Male
Name: en-HK-YanNeural, Gender: FemaleName: en-IE-ConnorNeural, Gender: Male
Name: en-IE-EmilyNeural, Gender: FemaleName: en-IN-NeerjaExpressiveNeural, Gender: Female
Name: en-IN-NeerjaNeural, Gender: FemaleName: en-IN-PrabhatNeural, Gender: Male
Name: en-KE-AsiliaNeural, Gender: FemaleName: en-KE-ChilembaNeural, Gender: Male
Name: en-NG-AbeoNeural, Gender: MaleName: en-NG-EzinneNeural, Gender: Female
Name: en-NZ-MitchellNeural, Gender: MaleName: en-NZ-MollyNeural, Gender: Female
Name: en-PH-JamesNeural, Gender: MaleName: en-PH-RosaNeural, Gender: Female
Name: en-SG-LunaNeural, Gender: FemaleName: en-SG-WayneNeural, Gender: Male
Name: en-TZ-ElimuNeural, Gender: MaleName: en-TZ-ImaniNeural, Gender: Female
Name: en-US-AnaNeural, Gender: FemaleName: en-US-AriaNeural, Gender: Female
Name: en-US-ChristopherNeural, Gender: MaleName: en-US-EricNeural, Gender: Male
Name: en-US-GuyNeural, Gender: MaleName: en-US-JennyNeural, Gender: Female
Name: en-US-MichelleNeural, Gender: FemaleName: en-US-RogerNeural, Gender: Male
Name: en-US-SteffanNeural, Gender: MaleName: en-ZA-LeahNeural, Gender: Female
Name: en-ZA-LukeNeural, Gender: MaleName: es-AR-ElenaNeural, Gender: Female
Name: es-AR-TomasNeural, Gender: MaleName: es-BO-MarceloNeural, Gender: Male
Name: es-BO-SofiaNeural, Gender: FemaleName: es-CL-CatalinaNeural, Gender: Female
Name: es-CL-LorenzoNeural, Gender: MaleName: es-CO-GonzaloNeural, Gender: Male
Name: es-CO-SalomeNeural, Gender: FemaleName: es-CR-JuanNeural, Gender: Male
Name: es-CR-MariaNeural, Gender: FemaleName: es-CU-BelkysNeural, Gender: Female
Name: es-CU-ManuelNeural, Gender: MaleName: es-DO-EmilioNeural, Gender: Male
Name: es-DO-RamonaNeural, Gender: FemaleName: es-EC-AndreaNeural, Gender: Female
Name: es-EC-LuisNeural, Gender: MaleName: es-ES-AlvaroNeural, Gender: Male
Name: es-ES-ElviraNeural, Gender: FemaleName: es-GQ-JavierNeural, Gender: Male
Name: es-GQ-TeresaNeural, Gender: FemaleName: es-GT-AndresNeural, Gender: Male
Name: es-GT-MartaNeural, Gender: FemaleName: es-HN-CarlosNeural, Gender: Male
Name: es-HN-KarlaNeural, Gender: FemaleName: es-MX-DaliaNeural, Gender: Female
Name: es-MX-JorgeNeural, Gender: MaleName: es-NI-FedericoNeural, Gender: Male
Name: es-NI-YolandaNeural, Gender: FemaleName: es-PA-MargaritaNeural, Gender: Female
Name: es-PA-RobertoNeural, Gender: MaleName: es-PE-AlexNeural, Gender: Male
Name: es-PE-CamilaNeural, Gender: FemaleName: es-PR-KarinaNeural, Gender: Female
Name: es-PR-VictorNeural, Gender: MaleName: es-PY-MarioNeural, Gender: Male
Name: es-PY-TaniaNeural, Gender: FemaleName: es-SV-LorenaNeural, Gender: Female
Name: es-SV-RodrigoNeural, Gender: MaleName: es-US-AlonsoNeural, Gender: Male
Name: es-US-PalomaNeural, Gender: FemaleName: es-UY-MateoNeural, Gender: Male
Name: es-UY-ValentinaNeural, Gender: FemaleName: es-VE-PaolaNeural, Gender: Female
Name: es-VE-SebastianNeural, Gender: MaleName: et-EE-AnuNeural, Gender: Female
Name: et-EE-KertNeural, Gender: MaleName: fa-IR-DilaraNeural, Gender: Female
Name: fa-IR-FaridNeural, Gender: MaleName: fi-FI-HarriNeural, Gender: Male
Name: fi-FI-NooraNeural, Gender: FemaleName: fil-PH-AngeloNeural, Gender: Male
Name: fil-PH-BlessicaNeural, Gender: FemaleName: fr-BE-CharlineNeural, Gender: Female
Name: fr-BE-GerardNeural, Gender: MaleName: fr-CA-AntoineNeural, Gender: Male
Name: fr-CA-JeanNeural, Gender: MaleName: fr-CA-SylvieNeural, Gender: Female
Name: fr-CH-ArianeNeural, Gender: FemaleName: fr-CH-FabriceNeural, Gender: Male
Name: fr-FR-DeniseNeural, Gender: FemaleName: fr-FR-EloiseNeural, Gender: Female
Name: fr-FR-HenriNeural, Gender: MaleName: ga-IE-ColmNeural, Gender: Male
Name: ga-IE-OrlaNeural, Gender: FemaleName: gl-ES-RoiNeural, Gender: Male
Name: gl-ES-SabelaNeural, Gender: FemaleName: gu-IN-DhwaniNeural, Gender: Female
Name: gu-IN-NiranjanNeural, Gender: MaleName: he-IL-AvriNeural, Gender: Male
Name: he-IL-HilaNeural, Gender: FemaleName: hi-IN-MadhurNeural, Gender: Male
Name: hi-IN-SwaraNeural, Gender: FemaleName: hr-HR-GabrijelaNeural, Gender: Female
Name: hr-HR-SreckoNeural, Gender: MaleName: hu-HU-NoemiNeural, Gender: Female
Name: hu-HU-TamasNeural, Gender: MaleName: id-ID-ArdiNeural, Gender: Male
Name: id-ID-GadisNeural, Gender: FemaleName: is-IS-GudrunNeural, Gender: Female
Name: is-IS-GunnarNeural, Gender: MaleName: it-IT-DiegoNeural, Gender: Male
Name: it-IT-ElsaNeural, Gender: FemaleName: it-IT-IsabellaNeural, Gender: Female
Name: ja-JP-KeitaNeural, Gender: MaleName: ja-JP-NanamiNeural, Gender: Female
Name: jv-ID-DimasNeural, Gender: MaleName: jv-ID-SitiNeural, Gender: Female
Name: ka-GE-EkaNeural, Gender: FemaleName: ka-GE-GiorgiNeural, Gender: Male
Name: kk-KZ-AigulNeural, Gender: FemaleName: kk-KZ-DauletNeural, Gender: Male
Name: km-KH-PisethNeural, Gender: MaleName: km-KH-SreymomNeural, Gender: Female
Name: kn-IN-GaganNeural, Gender: MaleName: kn-IN-SapnaNeural, Gender: Female
Name: ko-KR-InJoonNeural, Gender: MaleName: ko-KR-SunHiNeural, Gender: Female
Name: lo-LA-ChanthavongNeural, Gender: MaleName: lo-LA-KeomanyNeural, Gender: Female
Name: lt-LT-LeonasNeural, Gender: MaleName: lt-LT-OnaNeural, Gender: Female
Name: lv-LV-EveritaNeural, Gender: FemaleName: lv-LV-NilsNeural, Gender: Male
Name: mk-MK-AleksandarNeural, Gender: MaleName: mk-MK-MarijaNeural, Gender: Female
Name: ml-IN-MidhunNeural, Gender: MaleName: ml-IN-SobhanaNeural, Gender: Female
Name: mn-MN-BataaNeural, Gender: MaleName: mn-MN-YesuiNeural, Gender: Female
Name: mr-IN-AarohiNeural, Gender: FemaleName: mr-IN-ManoharNeural, Gender: Male
Name: ms-MY-OsmanNeural, Gender: MaleName: ms-MY-YasminNeural, Gender: Female
Name: mt-MT-GraceNeural, Gender: FemaleName: mt-MT-JosephNeural, Gender: Male
Name: my-MM-NilarNeural, Gender: FemaleName: my-MM-ThihaNeural, Gender: Male
Name: nb-NO-FinnNeural, Gender: MaleName: nb-NO-PernilleNeural, Gender: Female
Name: ne-NP-HemkalaNeural, Gender: FemaleName: ne-NP-SagarNeural, Gender: Male
Name: nl-BE-ArnaudNeural, Gender: MaleName: nl-BE-DenaNeural, Gender: Female
Name: nl-NL-ColetteNeural, Gender: FemaleName: nl-NL-FennaNeural, Gender: Female
Name: nl-NL-MaartenNeural, Gender: MaleName: pl-PL-MarekNeural, Gender: Male
Name: pl-PL-ZofiaNeural, Gender: FemaleName: ps-AF-GulNawazNeural, Gender: Male
Name: ps-AF-LatifaNeural, Gender: FemaleName: pt-BR-AntonioNeural, Gender: Male
Name: pt-BR-FranciscaNeural, Gender: FemaleName: pt-PT-DuarteNeural, Gender: Male
Name: pt-PT-RaquelNeural, Gender: FemaleName: ro-RO-AlinaNeural, Gender: Female
Name: ro-RO-EmilNeural, Gender: MaleName: ru-RU-DmitryNeural, Gender: Male
Name: ru-RU-SvetlanaNeural, Gender: FemaleName: si-LK-SameeraNeural, Gender: Male
Name: si-LK-ThiliniNeural, Gender: FemaleName: sk-SK-LukasNeural, Gender: Male
Name: sk-SK-ViktoriaNeural, Gender: FemaleName: sl-SI-PetraNeural, Gender: Female
Name: sl-SI-RokNeural, Gender: MaleName: so-SO-MuuseNeural, Gender: Male
Name: so-SO-UbaxNeural, Gender: FemaleName: sq-AL-AnilaNeural, Gender: Female
Name: sq-AL-IlirNeural, Gender: MaleName: sr-RS-NicholasNeural, Gender: Male
Name: sr-RS-SophieNeural, Gender: FemaleName: su-ID-JajangNeural, Gender: Male
Name: su-ID-TutiNeural, Gender: FemaleName: sv-SE-MattiasNeural, Gender: Male
Name: sv-SE-SofieNeural, Gender: FemaleName: sw-KE-RafikiNeural, Gender: Male
Name: sw-KE-ZuriNeural, Gender: FemaleName: sw-TZ-DaudiNeural, Gender: Male
Name: sw-TZ-RehemaNeural, Gender: FemaleName: ta-IN-PallaviNeural, Gender: Female
Name: ta-IN-ValluvarNeural, Gender: MaleName: ta-LK-KumarNeural, Gender: Male
Name: ta-LK-SaranyaNeural, Gender: FemaleName: ta-MY-KaniNeural, Gender: Female
Name: ta-MY-SuryaNeural, Gender: MaleName: ta-SG-AnbuNeural, Gender: Male
Name: ta-SG-VenbaNeural, Gender: FemaleName: te-IN-MohanNeural, Gender: Male
Name: te-IN-ShrutiNeural, Gender: FemaleName: th-TH-NiwatNeural, Gender: Male
Name: th-TH-PremwadeeNeural, Gender: FemaleName: tr-TR-AhmetNeural, Gender: Male
Name: tr-TR-EmelNeural, Gender: FemaleName: uk-UA-OstapNeural, Gender: Male
Name: uk-UA-PolinaNeural, Gender: FemaleName: ur-IN-GulNeural, Gender: Female
Name: ur-IN-SalmanNeural, Gender: MaleName: ur-PK-AsadNeural, Gender: Male
Name: ur-PK-UzmaNeural, Gender: FemaleName: uz-UZ-MadinaNeural, Gender: Female
Name: uz-UZ-SardorNeural, Gender: MaleName: vi-VN-HoaiMyNeural, Gender: Female
Name: vi-VN-NamMinhNeural, Gender: MaleName: zh-CN-XiaoxiaoNeural, Gender: Female
Name: zh-CN-XiaoyiNeural, Gender: FemaleName: zh-CN-YunjianNeural, Gender: Male
Name: zh-CN-YunxiNeural, Gender: MaleName: zh-CN-YunxiaNeural, Gender: Male
Name: zh-CN-YunyangNeural, Gender: MaleName: zh-CN-liaoning-XiaobeiNeural, Gender: Female
Name: zh-CN-shaanxi-XiaoniNeural, Gender: FemaleName: zh-HK-HiuGaaiNeural, Gender: Female
Name: zh-HK-HiuMaanNeural, Gender: FemaleName: zh-HK-WanLungNeural, Gender: Male
Name: zh-TW-HsiaoChenNeural, Gender: FemaleName: zh-TW-HsiaoYuNeural, Gender: Female
Name: zh-TW-YunJheNeural, Gender: MaleName: zu-ZA-ThandoNeural, Gender: Female
Name: zu-ZA-ThembaNeural, Gender: Male,
meal, food, bread time

Text Chunker

This script works its way through a long text file and “chunks” the text into smaller files, each with 100 sentences. You can easily change the length of the chunked files.

Usage

This is useful for creating smaller text files for summarisation, or for converting a long book into smaller sections and then using a text to speech script HERE to create bite-size audible files.

Prerequisites

You will need to install the “pdfminer” library first, for this to work:

pip install pdfminer.six

How to use it

  1. Save the script below in the folder of your choice
  2. Rename the text file you want to chunk as “test.txt” and save it in the same folder as the script
  3. Run the script
  4. The chunked files are saved in the “chunks” folder
  5. You can change the length of each chunk by adjusting the parameter currently set to “100”.
import nltk
import re
import os

# Define the path to the input file
input_path = 'test.txt'

# Read in the input file
with open(input_path, 'r', encoding='utf-8', errors='ignore') as f:
    text = f.read()
    text = text.replace('\uf0b7', '#')  # replace problematic character with #
	
# Remove line breaks and page breaks
text = re.sub(r'\n|\f', '', text)

# Use NLTK to split the text into individual sentences
sentences = nltk.sent_tokenize(text)

# Use regular expressions to split the sentences into chunks of 100
chunks = [sentences[i:i+100] for i in range(0, len(sentences), 100)]

# Create a directory to store the output files
if not os.path.exists('chunks'):
    os.mkdir('chunks')

# Loop through the chunks and save each one as a separate text file
for i, chunk in enumerate(chunks):
    chunk_text = ' '.join(chunk)
    chunk_num = i + 1
    output_path = f'chunks/chunk {chunk_num}.txt'
    with open(output_path, 'w') as f:
        f.write(chunk_text)
pdf, document, icon

.pdf to .txt Converter

This is a really simple but effective way to convert pdf files to txt files.

Prerequisites

You will need to install the “pdfminer” library first, for this to work:

pip install pdfminer.six

How to use it

  1. Save the script below in the folder of your choice
  2. Rename the .pdf file you want to convert as “test.pdf” and save it in the same folder as the script
  3. Run the script
  4. The text file is saved as “test.txt”
import io
import pdfminer.high_level
import pdfminer.layout

# Open the PDF file in read-binary mode
with open('test.pdf', 'rb') as pdf_file:

    # Use pdfminer to extract the text
    extracted_text = pdfminer.high_level.extract_text(pdf_file)

# Open a new text file in write mode
with open('test.txt', 'w', encoding='utf-8') as txt_file:

    # Write the extracted text to the text file
    txt_file.write(extracted_text)