version 0.0.10

This commit is contained in:
guanjihuan 2021-07-20 09:30:40 +08:00
parent 35b20ded5d
commit 37b196377f
4 changed files with 81 additions and 3 deletions

View File

@ -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) guan.plot_contour(x, y, matrix, xlabel='x', ylabel='y', title='', filename='a', show=1, save=0)
# download # download
guan.download_with_scihub(address=None, num=1) 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)

View File

@ -1,7 +1,7 @@
[metadata] [metadata]
# replace with your username: # replace with your username:
name = guan name = guan
version = 0.0.9 version = 0.0.10
author = guanjihuan author = guanjihuan
author_email = guanjihuan@163.com author_email = guanjihuan@163.com
description = An open source python package description = An open source python package

View File

@ -19,4 +19,5 @@ from .calculate_Chern_number import *
from .calculate_Wilson_loop import * from .calculate_Wilson_loop import *
from .read_and_write import * from .read_and_write import *
from .plot_figures import * from .plot_figures import *
from .download import * from .download import *
from .audio import *

72
PyPI/src/guan/audio.py Normal file
View File

@ -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()