当前位置: 首页 > news >正文

利用python实现文字转语音

代码如下:

import pathlib
import tkinter as tk
import tkinter.ttk as ttk
import tkinter.filedialog as filedialog
import tkinter.messagebox as msgbox
import pyttsx3class Application(tk.Tk):def __init__(self):super().__init__()self.title("文本-语音 转换器")self.geometry("300x120")self.engine = pyttsx3.init()self.file_path = tk.StringVar()self.init_ui()def init_ui(self):self.row1 = tk.LabelFrame(self, text="文本文件")self.txt_path = tk.Entry(self.row1, textvariable=self.file_path)self.txt_path.pack(side=tk.LEFT, fill=tk.X, expand=True, padx=5, pady=5)self.btn_sel = tk.Button(self.row1, text="选择", command=self.select_file)self.btn_sel.pack(side=tk.RIGHT, pady=5, padx=5)self.row1.pack(fill=tk.X, padx=5, pady=5)self.btn_convert = tk.Button(self, text="转换为 MP3", command=self.convert_to_mp3)self.btn_convert.pack(fill=tk.BOTH, expand=True, padx=5, pady=5)def select_file(self):txt_file = filedialog.askopenfile(initialdir=".", title="选择文件", filetypes=(('文件文件', '*.txt'),('所有文件', '*.*')))if txt_file:self.file_path.set(txt_file.name)def convert_to_mp3(self):filename = pathlib.Path(self.file_path.get())with open(filename, 'r', encoding='utf8') as f:self.engine.save_to_file(f.read(), f"{filename.stem}.mp3")self.engine.runAndWait()msgbox.showinfo("转换结果", "文件转换成功!")if __name__ == "__main__":app = Application()app.mainloop()
http://www.lryc.cn/news/333259.html

相关文章:

  • 拾光坞N3 ARM 虚拟主机 i茅台项目
  • docker安装nacos,单例模式(standalone),使用mysql数据库
  • 【运输层】传输控制协议 TCP
  • 深入浅出 -- 系统架构之Keepalived搭建双机热备
  • 如何做好产业园运营?树莓集团:响应政府号召,规划,注重大局观
  • NIO与BIO
  • YOLOv5实战记录05 Pyside6可视化界面
  • HTML5.Canvas简介
  • excel统计分析——多项式回归
  • SQLyog连接数据库8.0版本解析错误问题解决方案
  • 【数据库】SQL简介
  • AWS入门实践-利用S3构建一个静态网站
  • 使用Linux strace追踪系统调用: 一个详细指南
  • python 笔记
  • 软考 系统架构设计师系列知识点之数据库基本概念(4)
  • Vue - 3( 15000 字 Vue 入门级教程)
  • 上位机图像处理和嵌入式模块部署(qmacvisual图像清晰度)
  • 数据结构和算法:分治
  • 增强Java技能:使用OkHttp下载www.dianping.com信息
  • 用友 NC saveXmlToFIleServlet 任意文件上传漏洞复现
  • JS第九天
  • CSS设置字体样式
  • [Java线程池]ExecutorService|CompletionService的区别与选择
  • MySQL-SQL编写练习:基本的SELECT语句
  • C++经典面试题目(十九)
  • acwing算法提高之图论--SPFA找负环
  • I2C驱动实验:测试I2C驱动是否与设备匹配
  • 5560.树的直径
  • Decoupled Multimodal Distilling for Emotion Recognition 论文阅读
  • 【css】使用display:inline-block后,元素间存在4px的间隔