Código de Python - Generador de audio-libros.

Imágen de perfil
Val: 712
Bronce
Ha aumentado 1 puesto en Python (en relación al último mes)
Gráfica de Python

Generador de audio-libros.gráfica de visualizaciones


Python

Publicado el 31 de Mayo del 2022 por Antonio (75 códigos)
1.494 visualizaciones desde el 31 de Mayo del 2022
El programa muestra el contenido de texto de un pdf seleccionado por el usuario y permite la conversión de dicho texto a formato audio. También permite la selección de la página a mostrar mediante el botón "GO" o mediante los botones "<" y ">" situados en los extremos inferiores de la ventana. A su vez, también permite la selección del idioma para la pronunciación del texto en el audio que se genere.
PARA CUALQIER DUDA U OBSERVACIÓN, USEN LA SECCIÓN DE COMENTARIOS.
abm
abm2

Requerimientos

Lenguaje: Python
Librerías y recursos: tkinter, pdfminer, io, googletrans, threading, gtts, os, unicodedata

0.1

Publicado el 31 de Mayo del 2022gráfica de visualizaciones de la versión: 0.1
1.495 visualizaciones desde el 31 de Mayo del 2022
estrellaestrellaestrellaestrellaestrella
estrellaestrellaestrellaestrella
estrellaestrellaestrella
estrellaestrella
estrella

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from tkinter import *
from tkinter import filedialog, messagebox, ttk
import tkinter.scrolledtext as scrolledtext
from pdfminer.pdfinterp import PDFResourceManager, PDFPageInterpreter
from pdfminer.layout import LAParams
from pdfminer.converter import TextConverter
from io import StringIO
from googletrans import Translator
from pdfminer.pdfpage import PDFPage
import threading
from unicodedata import normalize
from gtts import gTTS
import os
 
LANGUAGES = {'af': 'afrikaans','sq': 'albanian','am': 'amharic','ar': 'arabic','hy': 'armenian',
'az': 'azerbaijani','eu': 'basque','be': 'belarusian','bn': 'bengali','bs': 'bosnian','bg': 'bulgarian',
'ca': 'catalan','ceb': 'cebuano','ny': 'chichewa','zh-cn': 'chinese (simplified)','zh-tw': 'chinese (traditional)',
'co': 'corsican','hr': 'croatian','cs': 'czech','da': 'danish','nl': 'dutch','en': 'english','eo': 'esperanto',
'et': 'estonian','tl': 'filipino','fi': 'finnish','fr': 'french','fy': 'frisian','gl': 'galician','ka': 'georgian',
'de': 'german','el': 'greek','gu': 'gujarati','ht': 'haitian creole','ha': 'hausa','haw': 'hawaiian','iw': 'hebrew',
'he': 'hebrew','hi': 'hindi','hmn': 'hmong','hu': 'hungarian','is': 'icelandic','ig': 'igbo','id': 'indonesian',
'ga': 'irish','it': 'italian','ja': 'japanese','jw': 'javanese','kn': 'kannada','kk': 'kazakh','km': 'khmer',
'ko': 'korean','ku': 'kurdish (kurmanji)','ky': 'kyrgyz','lo': 'lao','la': 'latin','lv': 'latvian','lt': 'lithuanian',
'lb': 'luxembourgish','mk': 'macedonian','mg': 'malagasy','ms': 'malay','ml': 'malayalam','mt': 'maltese','mi': 'maori',
'mr': 'marathi','mn': 'mongolian','my': 'myanmar (burmese)','ne': 'nepali','no': 'norwegian','or': 'odia','ps': 'pashto',
'fa': 'persian','pl': 'polish','pt': 'portuguese','pa': 'punjabi','ro': 'romanian','ru': 'russian','sm': 'samoan',
'gd': 'scots gaelic','sr': 'serbian','st': 'sesotho','sn': 'shona','sd': 'sindhi','si': 'sinhala','sk': 'slovak',
'sl': 'slovenian','so': 'somali','es': 'spanish','su': 'sundanese','sw': 'swahili','sv': 'swedish','tg': 'tajik',
'ta': 'tamil','te': 'telugu','th': 'thai','tr': 'turkish','uk': 'ukrainian','ur': 'urdu','ug': 'uyghur','uz': 'uzbek',
'vi': 'vietnamese','cy': 'welsh','xh': 'xhosa','yi': 'yiddish','yo': 'yoruba','zu': 'zulu'}
 
 
class App:
    def __init__(self):
 
        self.ventana = Tk()
        self.ventana.configure(bg='dim gray')
        self.ventana.geometry("1061x624")
        self.ventana.title("PDF-AUDIO-TEXT MAKER")
        self.translator = Translator()
        self.loaded = ""
        self.current_dir = StringVar()
        self.current_dir.set(os.getcwd())
        self.doc = StringVar()
        self.text = ""
        self.cancel = False
 
        self.resource_manager = PDFResourceManager(caching=True)
 
        Entry(self.ventana,textvariable=self.current_dir,bg='light gray',width=176).place(x=0,y=0)#.pack(side=TOP)
        Button(self.ventana,text="SEARCH PDF",command=self.init_task).place(x=9,y=28)
        Entry(self.ventana,textvariable=self.doc,width=18,font=("arial",14)).place(x=90,y=27)
        Button(self.ventana,text="GO",command=self.go_to_page).place(x=1028,y=30)
        Button(self.ventana,text="SAVE AUDIOBOOK",command=self.init_task2).place(x=309,y=28)
        Label(self.ventana,text="LANG:",bg="dim gray",fg="white").place(x=730,y=29)
        self.langList = ttk.Combobox(self.ventana,width=12)
        self.langList.place(x=769,y=29)
        Button(self.ventana,text="<",command=lambda:self.move(-1)).place(x=9,y=597)
        Button(self.ventana,text=">",command=lambda:self.move(1)).place(x=1036,y=597)
        Label(self.ventana,text="PAGES:",bg="dim gray",fg="white").place(x=888,y=29)
        self.pageList = ttk.Combobox(self.ventana,width=12)
        self.pageList.place(x=931,y=29)
        self.label2 = Label(self.ventana,bg='dim gray',fg='white',width=143)
        self.label2.place(x=28,y=599)
        self.display=scrolledtext.ScrolledText(self.ventana,background='white',width=128,height=33)#width=120,height=32
        self.display.place(x=9,y=62)
 
        self.keys = list(LANGUAGES.keys())
        self.langList["values"] = list(LANGUAGES.values())
 
        self.ventana.mainloop()
 
    def open_file(self):
        try:
            self.display.config(state=NORMAL)
            self.pdf_file = filedialog.askopenfilename(initialdir="/",title="SELECT FILE",
                                    filetypes=(("PDF files","*.pdf"),("all files","*.*")))
            if self.pdf_file:
                self.loaded = self.pdf_file
                self.pages = 0
                self.name = self.pdf_file.split('/')[-1]
                self.out_text = StringIO()
                self.codec_text = 'utf-8'
                self.laParams = LAParams()
                self.text_converter = TextConverter(self.resource_manager, self.out_text, codec=self.codec_text, laparams=self.laParams)
                self.interpreter = PDFPageInterpreter(self.resource_manager, self.text_converter)
 
                self.label2.configure(text="LOADING TEXT...(PAGES: {})".format(self.pages))
                with open(self.pdf_file, 'rb') as fp:
                    for page in PDFPage.get_pages(fp, pagenos=set(), maxpages=0, password="", caching=True, check_extractable=True):
                        self.interpreter.process_page(page)
                        self.label2.configure(text="LOADING TEXT...(PAGES: {})".format(self.pages))
                        self.pages += 1
 
                self.n_pages()
                self.text = self.out_text.getvalue()
                self.text = self.normalize_text()
                try:
                    self.lang = (self.translator.translate(self.text).src)
                except:
                    messagebox.showwarning("UNEXPECTED ERROR",'''There was a problem connecting to the Google Translator service. The language will default to 'English'.''')
                    self.lang = 'en'
                self.langList.set(LANGUAGES[self.lang])
                print(self.lang)
                self.display.delete('1.0', END)
                self.display.insert(END, self.text)
                self.display.config(state=DISABLED)##
 
                self.label2.configure(text="TITTLE: {} (PAGES: {})".format(self.name,self.pages))
                self.doc.set(self.name)
                #self.docName.configure(state="disabled")
            else:
                if self.loaded != "":
                    self.pdf_file = self.loaded
 
        except Exception as e:
            messagebox.showwarning("LOAD ERROR", str(e))
            self.label2.configure(text="")
 
    def normalize_text(self):
        trans_tab = dict.fromkeys(map(ord, u'\u0301\u0308'), None)
        self.text = normalize('NFKC', normalize('NFKD', self.text).translate(trans_tab))
        return self.text
 
    def go_to_page(self):
        if self.text != "":
            self.display.config(state=NORMAL)
            pages = 0
            self.display.delete('1.0', END)
            self.out_text = StringIO()
            self.text_converter = TextConverter(self.resource_manager, self.out_text, codec=self.codec_text, laparams=self.laParams)
            self.interpreter = PDFPageInterpreter(self.resource_manager, self.text_converter)
            with open(self.pdf_file, 'rb') as fp:
                for page in PDFPage.get_pages(fp, pagenos=set(), maxpages=0, password="", caching=True, check_extractable=True):
                    if self.pageList.get() == "ALL PAGES":
                        self.interpreter.process_page(page)
                    else:
                        if pages == int(self.pageList.get().split(' ')[-1])-1:
                            print(pages)
                            self.interpreter.process_page(page)
                            break
                    pages+=1
 
            self.text = self.out_text.getvalue()
            self.text = self.normalize_text()
            if self.pageList.get() != "ALL PAGES":
                self.display.insert(END, "*"*60+"PAGE: {}".format(pages+1)+"*"*60+"\n")
            self.display.insert(END, self.text)
            self.display.config(state=DISABLED)
 
    def init_task(self):
        t = threading.Thread(target=self.open_file)
        t.start()
 
    def n_pages(self):
        self.list_of_pages = []
        for i in range(self.pages):
            self.list_of_pages.append("PAGE {}".format(i+1))
        self.list_of_pages.append("ALL PAGES")
        self.pageList["values"] = self.list_of_pages
        self.pageList.set("ALL PAGES")
 
    def move(self,mov):
        if self.text != "":
            current_pos = self.list_of_pages.index(self.pageList.get())
            if current_pos + 1 == len(self.list_of_pages):
                current_pos = -1
            self.pageList.set(self.list_of_pages[current_pos+(mov)])
 
            self.go_to_page()
            print(current_pos)
 
    def init_task2(self):
        if self.text != "":
            t = threading.Thread(target=self.create_audio_file)
            t.start()
        else:
            messagebox.showwarning("NO INFO","No text to convert.")
 
    def create_audio_file(self):
        audio_book = filedialog.asksaveasfilename(initialdir="/",title="Save as",defaultextension=".mp3")
        if audio_book:
            self.label2.configure(text="SAVING: {}".format(audio_book.split('/')[-1]))
            try:
                chosen_lan = list(LANGUAGES.keys())[list(LANGUAGES.values()).index(self.langList.get())]
                tts = gTTS(self.text,lang=chosen_lan)
                tts.save(audio_book)
                self.label2.configure(text="TITTLE: {} (PAGES: {})".format(self.name,self.pages))
                messagebox.showinfo("SAVED","Saved file '{}'".format(audio_book.split('/')[-1]))
            except Exception as e:
                messagebox.showwarning("UNEXPECTED ERROR",str(e))
 
if __name__=="__main__":
    App()



Comentarios sobre la versión: 0.1 (0)


No hay comentarios
 

Comentar la versión: 0.1

Nombre
Correo (no se visualiza en la web)
Valoración
Comentarios...
CerrarCerrar
CerrarCerrar
Cerrar

Tienes que ser un usuario registrado para poder insertar imágenes, archivos y/o videos.

Puedes registrarte o validarte desde aquí.

Codigo
Negrita
Subrayado
Tachado
Cursiva
Insertar enlace
Imagen externa
Emoticon
Tabular
Centrar
Titulo
Linea
Disminuir
Aumentar
Vista preliminar
sonreir
dientes
lengua
guiño
enfadado
confundido
llorar
avergonzado
sorprendido
triste
sol
estrella
jarra
camara
taza de cafe
email
beso
bombilla
amor
mal
bien
Es necesario revisar y aceptar las políticas de privacidad

http://lwp-l.com/s7238