| import asyncio |
| import edge_tts |
| import pandas as pd |
| import os |
| from tqdm import tqdm |
|
|
| |
| INPUT_FILE = "" |
| OUTPUT_DIR = "" |
| VOICE = "en-US-GuyNeural" |
| |
|
|
| async def generate_speech(code, text, output_path): |
| """ |
| Generate one speech clip. |
| """ |
| try: |
| communicate = edge_tts.Communicate(text, VOICE) |
| await communicate.save(output_path) |
| |
| tqdm.write(f"[OK] Saved {code}.mp3") |
| return True |
| except Exception as e: |
| tqdm.write(f"[FAIL] {code}.mp3: {e}") |
| return False |
|
|
| async def amain(): |
| |
| if not os.path.exists(OUTPUT_DIR): |
| os.makedirs(OUTPUT_DIR) |
| tqdm.write(f"Created output directory: {OUTPUT_DIR}") |
|
|
| |
| try: |
| df = pd.read_excel(INPUT_FILE) |
| |
| df.columns = df.columns.str.strip() |
| if 'code' not in df.columns or 'sentence' not in df.columns: |
| tqdm.write("Error: Excel must contain 'code' and 'sentence' columns") |
| return |
| except FileNotFoundError: |
| tqdm.write(f"Error: file not found: {INPUT_FILE}") |
| return |
| except Exception as e: |
| tqdm.write(f"Error reading Excel: {e}") |
| return |
|
|
| total_count = len(df) |
| tqdm.write(f"Starting: {total_count} row(s)...") |
|
|
| |
| |
| |
| |
| for index, row in tqdm(df.iterrows(), total=total_count, desc="Progress", unit="row"): |
| code = str(row['code']).strip() |
| sentence = str(row['sentence']).strip() |
|
|
| |
| if not sentence: |
| tqdm.write(f"[SKIP] row {index}: empty sentence") |
| continue |
|
|
| |
| safe_code = "".join([c for c in code if c not in r'\/:*?"<>|']) |
| |
| |
| output_file = os.path.join(OUTPUT_DIR, f"{safe_code}.mp3") |
|
|
| |
| await generate_speech(safe_code, sentence, output_file) |
|
|
| tqdm.write("All tasks finished.") |
|
|
| if __name__ == "__main__": |
| asyncio.run(amain()) |