From 37b196377f3b90204276f57793aed4fbe75e0f7e Mon Sep 17 00:00:00 2001 From: guanjihuan <34735497+guanjihuan@users.noreply.github.com> Date: Tue, 20 Jul 2021 09:30:40 +0800 Subject: [PATCH] version 0.0.10 --- API_reference.py | 7 +++- PyPI/setup.cfg | 2 +- PyPI/src/guan/__init__.py | 3 +- PyPI/src/guan/audio.py | 72 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 81 insertions(+), 3 deletions(-) create mode 100644 PyPI/src/guan/audio.py diff --git a/API_reference.py b/API_reference.py index 798bfd3..544607c 100644 --- a/API_reference.py +++ b/API_reference.py @@ -115,4 +115,9 @@ guan.plot_3d_surface(x, y, matrix, xlabel='x', ylabel='y', zlabel='z', title='', guan.plot_contour(x, y, matrix, xlabel='x', ylabel='y', title='', filename='a', show=1, save=0) # download -guan.download_with_scihub(address=None, num=1) \ No newline at end of file +guan.download_with_scihub(address=None, num=1) + +# audio +guan.txt_to_audio(txt_path, rate=125, voice_type_0_or_1=1, read=1, save=0, print_text=0) +content = guan.pdf_to_text(pdf_path) +guan.pdf_to_audio(pdf_path, rate=125, voice_type_0_or_1=1, read=1, save=0, print_text=0) \ No newline at end of file diff --git a/PyPI/setup.cfg b/PyPI/setup.cfg index 7926eab..6f1eb52 100644 --- a/PyPI/setup.cfg +++ b/PyPI/setup.cfg @@ -1,7 +1,7 @@ [metadata] # replace with your username: name = guan -version = 0.0.9 +version = 0.0.10 author = guanjihuan author_email = guanjihuan@163.com description = An open source python package diff --git a/PyPI/src/guan/__init__.py b/PyPI/src/guan/__init__.py index fc5b9bb..b712326 100644 --- a/PyPI/src/guan/__init__.py +++ b/PyPI/src/guan/__init__.py @@ -19,4 +19,5 @@ from .calculate_Chern_number import * from .calculate_Wilson_loop import * from .read_and_write import * from .plot_figures import * -from .download import * \ No newline at end of file +from .download import * +from .audio import * \ No newline at end of file diff --git a/PyPI/src/guan/audio.py b/PyPI/src/guan/audio.py new file mode 100644 index 0000000..6de0a75 --- /dev/null +++ b/PyPI/src/guan/audio.py @@ -0,0 +1,72 @@ +# GUAN is an open-source python package developed and maintained by https://www.guanjihuan.com. The primary location of this package is on website https://py.guanjihuan.com. + +# audio + +def txt_to_audio(txt_path, rate=125, voice_type_0_or_1=1, read=1, save=0, print_text=0): + import pyttsx3 + f = open(txt_path, 'r', encoding ='utf-8') + text = f.read() + if print_text==1: + print(text) + engine = pyttsx3.init() + voices = engine.getProperty('voices') + engine.setProperty('voice', voices[voice_type_0_or_1].id) + engine.setProperty("rate", rate) + if save==1: + import re + file_name = re.split('[/,\\\]', txt_path)[-1][:-4] + engine.save_to_file(text, file_name+'.mp3') + engine.runAndWait() + print('MP3 file saved!') + if read==1: + engine.say(text) + engine.runAndWait() + +def pdf_to_text(pdf_path): + from pdfminer.pdfparser import PDFParser, PDFDocument + from pdfminer.pdfinterp import PDFResourceManager, PDFPageInterpreter + from pdfminer.converter import PDFPageAggregator + from pdfminer.layout import LAParams, LTTextBox + from pdfminer.pdfinterp import PDFTextExtractionNotAllowed + import logging + logging.Logger.propagate = False + logging.getLogger().setLevel(logging.ERROR) + praser = PDFParser(open(pdf_path, 'rb')) + doc = PDFDocument() + praser.set_document(doc) + doc.set_parser(praser) + doc.initialize() + if not doc.is_extractable: + raise PDFTextExtractionNotAllowed + else: + rsrcmgr = PDFResourceManager() + laparams = LAParams() + device = PDFPageAggregator(rsrcmgr, laparams=laparams) + interpreter = PDFPageInterpreter(rsrcmgr, device) + content = '' + for page in doc.get_pages(): + interpreter.process_page(page) + layout = device.get_result() + for x in layout: + if isinstance(x, LTTextBox): + content = content + x.get_text().strip() + return content + +def pdf_to_audio(pdf_path, rate=125, voice_type_0_or_1=1, read=1, save=0, print_text=0): + import pyttsx3 + text = pdf_to_text(pdf_path) + if print_text==1: + print(text) + engine = pyttsx3.init() + voices = engine.getProperty('voices') + engine.setProperty('voice', voices[voice_type_0_or_1].id) + engine.setProperty("rate", rate) + if save==1: + import re + file_name = re.split('[/,\\\]', pdf_path)[-1][:-4] + engine.save_to_file(text, file_name+'.mp3') + engine.runAndWait() + print('MP3 file saved!') + if read==1: + engine.say(text) + engine.runAndWait() \ No newline at end of file