import win32ui
import time
import requests
API_ID="31"
API_KEY = "9cLN3vGG6"
SECRET_KEY = "G0w863ga9p0uNULIM7"
from concurrent.futures import ThreadPoolExecutor
import win32api
import win32print
import os,glob
import collections
import time
import threading
import shutil
from threading import Thread
def ospath(folder_path):
if os.path.isdir(folder_path):
for file in glob.glob(folder_path+'\*'):
os.remove(file)
#os.rmdir(folder_path)
else:
# file not found message
print("File not found in the directory")
#os.mkdir(folder_path)
src_dir='E:\\FTP\\'
dst_dir='E:\\FTPTO\\'
ospath(src_dir)
ospath(dst_dir)
time.sleep(4)
def myprinter(src_dir,dst_dir):
#目的路径记得加斜杠
src_file_list = glob.glob(src_dir + '*')
#printre_name = "Gprinter GP-1324D"
for srcfile in src_file_list:
shutil.move (srcfile, dst_dir)
#dlinkprint(dst_dirfile)
newfile=srcfile.replace(src_dir,dst_dir)
print(srcfile+'--ok!!-the pdf prints normally---' + newfile)
if newfile.split(".")[1] in ['pdf', 'txt', 'xlsx', 'md', 'word']:
t921=Thread(target=dlinkprint(newfile),name='tcpsand1')#8路3定时5秒
t921.start()
t9211=Thread(target=TTSFile(newfile),name='tcpsand2')#8路3定时5秒
t9211.start()
def dlinkprint(filename):
printer_name = win32print.GetDefaultPrinter()
win32api.ShellExecute(0,"print",filename,'/d:"%s"' % printer_name,".",0)
from urllib.parse import urlencode
def TTS(text, speed, person,volume):
url = "https://tsn.baidu.com/text2audio"
url = "https://tts.baidu.com/text2audio"
payloadtext={'tex': text,
'tok': get_access_token(),
'cuid':API_ID,
'ctp':'1',
'lan':'zh',
'spd':speed,#语速,取值0-15,默认为5中语速
'pit':'6',#音调,取值0-15,默认为5中语调
'vol':volume,#声音大1——5小
'per':person,
'aue':'3'
}
"""
per 度小宇=1,度小美=0,度逍遥(基础)=3,度丫丫=4,
度逍遥(精品)=5003,度小鹿=5118,度博文=106,
度小童=110,度小萌=111,度米朵=103,度小娇=5
3为mp3格式(默认); 4为pcm-16k;5为pcm-8k;
6为wav(内容同pcm-16k); 注意aue=4或者6是语音识别要求的格式,
但是音频内容不是语音识别要求的自然人发音,所以识别效果会受影响。
"""
payload=urlencode(payloadtext)
#payload=urlencode(payload1)
headers = {
'Content-Type': 'application/x-www-form-urlencoded',
'Accept': '*/*'
}
result = requests.request("POST", url, headers=headers, data=payload)
try:
result.json()
except:
return result.content
else:
raise ValueError
def TTSFile(newfile):
text_messages=open(newfile, 'r' ,encoding="utf-8").read()
newmp3=newfile.replace('.txt','.mp3')
voice_data = TTS(text_messages, 7, 4, 9) #语速、不同人、音量
with open(newmp3, 'wb+') as f: #保存为.MP3文件
f.write(voice_data)
f.close()
#time.sleep(3)
from playsound import playsound
playsound(newmp3)
def get_access_token():
"""
使用 AK,SK 生成鉴权签名(Access Token)
:return: access_token,或是None(如果错误)
"""
url = "https://aip.baidubce.com/oauth/2.0/token"
params = {"grant_type": "client_credentials", "client_id": API_KEY, "client_secret": SECRET_KEY}
return str(requests.post(url, params=params).json().get("access_token"))
def print_ts(src_dir,dst_dir):
while True:
myprinter(src_dir,dst_dir)
print(src_dir,dst_dir)
time.sleep(3)
if __name__=="__main__":
print_ts(src_dir,dst_dir)