corizhang 2023-11-07 17:28:34 +08:00
parent 94f1683989
commit bd8f94353a
2 changed files with 64 additions and 38 deletions

3
test.py Normal file
View File

@ -0,0 +1,3 @@
tlist = []
tlist.insert(0,'abc')
print(tlist)

View File

@ -3,48 +3,66 @@ import pandas as pd
import wave import wave
import struct import struct
# 补全章节位数
def add_zero_to_title(chapterName, places):
"""
:param chapterName: 章节名称
:param places: 章节编号规定的位数, 如003, 即命名时需要的位数为3位
:return: 补全位数的章节编号
"""
while len(chapterName) < places:
chapterName = "0" + chapterName
return chapterName
# 打开Excel文件 # 打开Excel文件
def add_zero_to_title(title): def get_wav_title(filePath, selectedColumnList, sheetName, cvName, places):
while len(title) < 3: """
title = "0" + title :param filePath: 剧组文件的路径
return title :param selectedColumnList: 选择要保留的数据列
:param sheetName: 要读取的sheet名称
def get_data(file_path, column_list, cv_name): :param cvName: cv名称
excel_data = pd.read_excel(file_path) :return:
column_names = excel_data.columns.tolist() """
title = column_names[0] role_data = pd.read_excel(filePath, sheet_name=sheetName)
excel_data[title] = excel_data[title].ffill() role_data[selectedColumnList[0]] = role_data[selectedColumnList[0]].ffill()
column_cv = column_names[2] filtered_role_data = role_data[role_data[selectedColumnList[1]].astype(str).str.contains(cvName, na=False)]
print(column_cv) # filtered_role_data = role_data[role_data[selectedColumnList[1]].str.contains(cvName, na=False)]
filter_value = cv_name print(filtered_role_data)
filtered_data = excel_data[excel_data[column_cv].astype(str).str.contains(filter_value, na=False)]
print(filtered_data)
wav_title_list = [] wav_title_list = []
# 逐行读取指定列的数据 # 逐行读取指定列的数据
for index, row in filtered_data.iterrows(): for index, row in filtered_role_data.iterrows():
title = row[column_list[0]] title = row[selectedColumnList[0]]
title = re.findall(r"\d+\.?\d*", title) title = re.findall(r"\d+\.?\d*", title)
title = add_zero_to_title(title[0]) title = add_zero_to_title(title[0], places)
role = row[column_list[1]] roleName = row[selectedColumnList[2]]
num = row[column_list[2]] linesNum = str(row[selectedColumnList[3]])
print(num) if '.' in linesNum:
# wav_title = title[0] + '-' + role + '-' + cv_name + '-' + str(num)[:-2] wav_title = title + '-' + roleName + '-' + cvName + '-' + linesNum[:-2]
wav_title = title + '-' + role + '-' + cv_name + '-' + str(num) elif linesNum == 'nan':
wav_title = title + '-' + roleName + '-' + cvName + '-' + '0'
else:
wav_title = title + '-' + roleName + '-' + cvName + '-' + linesNum
wav_title_list.append(wav_title) wav_title_list.append(wav_title)
print(wav_title_list)
return wav_title_list return wav_title_list
def creat_wav_file(wav_title_list):
# 设置音频参数 def creat_wav_file(wav_title_list, sample_width, sample_rate, num_channels, save_path):
sample_width = 3 # 24位深度为3个字节 """
sample_rate = 44100 # 采样率为44100Hz :param wav_title_list: 波形文件名列表
num_channels = 1 # 单声道 :param sample_width: 位深度24位深度为3个字节
:param sample_rate: 采样率
:param num_channels: 声道1为单声道2为立体声
:return:
"""
# 创建WAV文件 # 创建WAV文件
for file_name in wav_title_list: for file_name in wav_title_list:
with wave.open(r"G:\ready\绝天神印\{}.wav".format(file_name), 'w') as wav_file: wav_name = save_path + '/' + file_name + '.wav'
with wave.open(wav_name, 'w') as wav_file:
# 设置音频参数 # 设置音频参数
wav_file.setsampwidth(sample_width) wav_file.setsampwidth(sample_width)
wav_file.setframerate(sample_rate) wav_file.setframerate(sample_rate)
@ -59,11 +77,16 @@ def creat_wav_file(wav_title_list):
wav_file.writeframes(sample) wav_file.writeframes(sample)
print('已成功创建{}文件'.format(file_name)) print('已成功创建{}文件'.format(file_name))
if __name__ == '__main__':
file_path = r"C:\Users\Administrator\Downloads\aadd.xlsx"
column_list = ['章节名称', '角色名', '台词量']
cv_name = '请叫我张钧'
wav_title_list = get_data(file_path, column_list, cv_name)
print(wav_title_list)
creat_wav_file(wav_title_list)
if __name__ == '__main__':
file_path = r"C:\Users\Cori\Downloads\逍遥小医仙剧组 (1).xlsx"
# column_list = ['章节名称', 'CV名称', '角色名', '台词量']
column_list = ['章节名', 'CV', '角色名', '台词数']
cv_name = '请叫我张钧'
sheetName = '角色音进度表201-487'
sample_width = 3 # 24位深度为3个字节
sample_rate = 44100 # 采样率为44100Hz
num_channels = 1 # 单声道
wav_title_list = get_wav_title(file_path, column_list, sheetName, cv_name, 4)
print(wav_title_list, sample_width, sample_rate, num_channels)
# creat_wav_file(wav_title_list)