from boto3 import Session
from openpyxl import load_workbook
from botocore.exceptions import BotoCoreError, ClientError
from contextlib import closing
import os
import sys
import subprocess
from tempfile import gettempdir
import pingcap.pe.flash.util as util
valid_col_names = [
"随机字母",
"随机数字",
"随机组合6位",
"随机组合8位",
"随机组合10位",
"收发信机台",
"收信机",
"某台移动到某地区工作",
]
aerocode_to_pronounce = {
"A": "Alpha",
"B": "Bravo",
"C": "Charlie",
"D": "Delta",
"E": "Echo",
"F": "Foxtrot",
"G": "Golf",
"H": "Hotel",
"I": "India",
"J": "Juliet",
"K": "Kilo",
"L": "Lima",
"M": "Mike",
"N": "November",
"O": "Oscar",
"P": "Papa",
"Q": "Quebec",
"R": "Romeo",
"S": "Sierra",
"T": "Tango",
"U": "Uniform",
"V": "Victor",
"W": "Whiskey",
"X": "X-ray",
"Y": "Yankee",
"Z": "Zulu",
"φ": "0",
"1": "1",
"2": "2",
"3": "3",
"4": "4",
"5": "5",
"6": "6",
"7": "7",
"8": "8",
"9": "9",
"/": "Slash",
}
session = util.get_boto_session()
polly = session.client("polly")
def process_cols(xlsx_file_path: str) -> list:
element_break = "" * 1
phase_wait_2 = '' * 1
phase_wait_3 = '' * 1
phase_wait_4 = '' * 1
phase_waits = [phase_wait_3]
col_wait = '' * 1
wb = load_workbook(xlsx_file_path, data_only=True)
for col in wb.active.columns:
for phase_wait in phase_waits:
col_values = []
for cell in col:
if cell != None and cell.value != None and cell.value != "None":
if cell.value == col[0].value:
print(cell.value)
audit_file_name_prefix = (
cell.value
+ "_"
+ "间隔"
+ phase_wait.replace('', "秒_"
)
)
else:
code_pronounce_array = [
aerocode_to_pronounce[i] + element_break
for i in str(cell.value)
]
for i in str(cell.value):
if i == "/":
c = "+"
else:
c = i
audit_file_name_prefix += c
audit_file_name_prefix += "-"
text = " ".join(code_pronounce_array)
col_values += [text + phase_wait]
col_text = " ".join(col_values) + col_wait
print("X: ", col_text)
try:
response = polly.synthesize_speech(
Text=''
+ col_text
+ "",
Engine="neural",
OutputFormat="mp3",
VoiceId="Brian",
TextType="ssml",
)
except (BotoCoreError, ClientError) as error:
print(error)
sys.exit(-1)
if "AudioStream" in response:
with closing(response["AudioStream"]) as stream:
output = os.path.join(audit_file_name_prefix + ".mp3")
try:
with open(output, "wb") as file:
file.write(stream.read())
except IOError as error:
print(error)
sys.exit(-1)
else:
print("Could not stream audio")
sys.exit(-1)
input_file_name = sys.argv[1]
process_cols(input_file_name)