add import excel menu

parent 514ecd33
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import requests
import json
import time
import urllib
import hmac
import base64
import hashlib
import urllib.parse
user_name = "盲图"
dd_url_prefix = "https://oapi.dingtalk.com/robot/send?access_token=a7e6b223bc0a6e4b5e3f64b1570d0c884db0d833592f5e84b020c2896130d659"
secret = 'SEC252452789bc17cfb1e6c973f837426fa0f9fd50c8793239c070e9d3c021d8109'
def notify(err):
timestamp = str(round(time.time() * 1000))
secret_enc = secret.encode('utf-8')
string_to_sign = '{}\n{}'.format(timestamp, secret)
string_to_sign_enc = string_to_sign.encode('utf-8')
hmac_code = hmac.new(secret_enc, string_to_sign_enc, digestmod=hashlib.sha256).digest()
sign = urllib.parse.quote_plus(base64.b64encode(hmac_code))
print(timestamp)
print(sign)
dd_url = dd_url_prefix + "&timestamp=" + timestamp + "&sign=" + sign
h = {"Content-Type": "application/json"}
d = {
"msgtype": "markdown",
"markdown": {
"title": "msg",
"text": "| 用户名称:%s \n" % (user_name) +
"\n" +
"| 异常时间:%s \n" % (time.strftime('%Y:%m:%d %H:%M:%S', time.localtime(int(time.time())))) +
"\n" +
"| 异常信息:%s \n" % (err)
}
}
r = requests.post(dd_url, json.dumps(d), headers=h)
print(r.status_code)
print(r.text)
\ No newline at end of file
import xlrd
import openpyxl
from management import Element
def read_xls(file_path):
print("read_xls")
elements = []
workboot = xlrd.open_workbook(file_path)
sheet = workboot.sheet_by_index(0)
row_index = 0
for row in range(sheet.nrows):
row_index = row_index + 1
if row_index == 1:
continue
col_index = 0
start_time = ""
end_time = ""
subtitle = ""
suggest = ""
aside = ""
speed = ""
for col in range(sheet.ncols):
cell_value = sheet.cell_value(row, col)
print(cell_value)
if col_index == 0:
start_time = cell_value if cell_value != None else ""
if col_index == 2:
end_time = cell_value if cell_value != None else ""
if col_index == 4:
subtitle = cell_value if cell_value != None else ""
if col_index == 5:
if cell_value == None or cell_value == "":
break
suggest = cell_value if cell_value != None else ""
if col_index == 6:
if cell_value == None or cell_value == "":
break
aside = cell_value if cell_value != None else ""
if col_index == 7:
speed = cell_value if cell_value != None else ""
col_index = col_index + 1
if suggest != "" and aside != "":
new_element = Element(start_time, end_time, subtitle, suggest, aside, speed)
new_element.print_self()
elements.append(new_element)
checkLength(elements)
return elements
def read_xlsx(file_path):
print("read_xlsx")
elements = []
workbook = openpyxl.load_workbook(file_path)
sheet = workbook.active
row_index = 0
for row in sheet.iter_rows():
row_index = row_index + 1
if row_index == 1:
continue
col_index = 0
start_time = ""
end_time = ""
subtitle = ""
suggest = ""
aside = ""
speed = ""
for cell in row:
cell_value = cell.value
print(cell_value)
if col_index == 0:
start_time = cell_value if cell_value != None else ""
if col_index == 2:
end_time = cell_value if cell_value != None else ""
if col_index == 4:
subtitle = cell_value if cell_value != None else ""
if col_index == 5:
if cell_value == None or cell_value == "":
break
suggest = cell_value if cell_value != None else ""
if col_index == 6:
if cell_value == None or cell_value == "":
break
aside = cell_value if cell_value != None else ""
if col_index == 7:
speed = cell_value if cell_value != None else ""
col_index = col_index + 1
if suggest != "" and aside != "":
new_element = Element(start_time, end_time, subtitle, suggest, aside, speed)
new_element.print_self()
elements.append(new_element)
checkLength(elements)
return elements
def checkLength(elements):
for element in elements:
if int("".join(filter(str.isdigit, element.suggest))) < len(element.aside):
raise Exception("旁白字数没有按照推荐要求")
# elements = read_xlsx("C:/Users/AIA/Desktop/1/3/1.xlsx")
# print(len(elements))
# for element in elements:
# element.print_self()
# print(type("".join(filter(str.isdigit, element.suggest))))
# print(len(element.aside))
# arrays = [1,2,3,4,5]
# arrays = arrays.pop(2)
# print(arrays)
# Args:
# row (int): 在该行后添加信息
# start_time (str): 起始时间
# end_time (str): 终止时间
# subtitle (str): 字幕文本
# suggest (str): 建议
# aside (str): 旁白文本
# speed (str): 旁白语速
# """
# # 注意,这里需要用同一对象,不能生成多个Element
# new_element = Element(start_time, end_time, subtitle, suggest, aside, speed)
\ No newline at end of file
...@@ -37,6 +37,10 @@ import copy ...@@ -37,6 +37,10 @@ import copy
import sys import sys
import re import re
from ding_utils import notify
from excel_utils import read_xls,read_xlsx
class MainWindow(QMainWindow, Ui_MainWindow): class MainWindow(QMainWindow, Ui_MainWindow):
EXIT_CODE_REBOOT = -12345678 EXIT_CODE_REBOOT = -12345678
...@@ -132,6 +136,8 @@ class MainWindow(QMainWindow, Ui_MainWindow): ...@@ -132,6 +136,8 @@ class MainWindow(QMainWindow, Ui_MainWindow):
self.action_3.setEnabled(False) self.action_3.setEnabled(False)
self.action_4.triggered.connect(self.show_assemble_dialog) self.action_4.triggered.connect(self.show_assemble_dialog)
self.action_4.setEnabled(False) self.action_4.setEnabled(False)
self.action_5.triggered.connect(self.import_excel)
self.action_5.setEnabled(False)
self.action_create.triggered.connect(self.show_create_dialog) # 新建工程 self.action_create.triggered.connect(self.show_create_dialog) # 新建工程
self.action_save.triggered.connect(self.save_project) self.action_save.triggered.connect(self.save_project)
self.action_save.setEnabled(False) self.action_save.setEnabled(False)
...@@ -434,6 +440,50 @@ class MainWindow(QMainWindow, Ui_MainWindow): ...@@ -434,6 +440,50 @@ class MainWindow(QMainWindow, Ui_MainWindow):
self.action_insert_aside_from_now.setEnabled(True) self.action_insert_aside_from_now.setEnabled(True)
self.insert_aside_from_now_btn.setEnabled(True) self.insert_aside_from_now_btn.setEnabled(True)
#导入旁白excel
def import_excel(self):
# excel_path = self.openExcelFile()
# print(excel_path)
# elements = None
# if excel_path.endswith("xls"):
# elements = read_xls(excel_path)
# else:
# elements = read_xlsx(excel_path)
# if elements != None and len(elements) > 0:
# self.batch_add_line_operation_slot(elements)
try:
excel_path = self.openExcelFile()
print(excel_path)
elements = None
if excel_path.endswith("xls"):
elements = read_xls(excel_path)
else:
elements = read_xlsx(excel_path)
if elements != None and len(elements) > 0:
self.batch_add_line_operation_slot(elements)
except Exception as e:
print("import excel err")
print(e)
notify(e)
self.prompt_dialog.show_with_msg("导入旁白失败,请检查模板及内容: " + e.args[0])
"""导入视频,并对主界面中的相关控件进行对应更新
"""
# video_path = self.openVideoFile().path()
# print("[import_slot] video_path=" + video_path)
# if video_path == "" or video_path == None:
# return
# if video_path[0] == '/' and ":" in video_path:
# video_path = video_path[1:]
# self.projectContext.video_path = video_path
# excel_name = os.path.splitext(os.path.basename(video_path))[0]
# self.projectContext.excel_path = os.path.join(
# self.projectContext.project_base_dir, excel_name + ".xlsx")
# self.action_export.setEnabled(True)
# self.action_operate.setEnabled(True)
# self.action_insert_aside_from_now.setEnabled(True)
# self.insert_aside_from_now_btn.setEnabled(True)
# todo: 后续这段代码公共的可以抽出来 # todo: 后续这段代码公共的可以抽出来
# 打开某个工程 # 打开某个工程
...@@ -461,6 +511,7 @@ class MainWindow(QMainWindow, Ui_MainWindow): ...@@ -461,6 +511,7 @@ class MainWindow(QMainWindow, Ui_MainWindow):
self.action_export.setEnabled(True) self.action_export.setEnabled(True)
self.action_3.setEnabled(True) self.action_3.setEnabled(True)
self.action_4.setEnabled(True) self.action_4.setEnabled(True)
self.action_5.setEnabled(True)
self.action_save.setEnabled(True) self.action_save.setEnabled(True)
self.action_operate.setEnabled(True) self.action_operate.setEnabled(True)
if self.projectContext.excel_path is not None: if self.projectContext.excel_path is not None:
...@@ -637,7 +688,8 @@ class MainWindow(QMainWindow, Ui_MainWindow): ...@@ -637,7 +688,8 @@ class MainWindow(QMainWindow, Ui_MainWindow):
for t in self.threads: for t in self.threads:
if t.exitcode != 0: if t.exitcode != 0:
print("Exception in", t.getName()) print("Exception in", t.getName())
self.show_warning_msg_box("运行出错,请联系开发者处理") self.show_warning_msg_box(t.exc_traceback)
notify(t.exc_traceback)
return return
self.statusbarLabel.setText(" %s完成" % (type)) self.statusbarLabel.setText(" %s完成" % (type))
self.progressBar.setValue(100) self.progressBar.setValue(100)
...@@ -820,6 +872,14 @@ class MainWindow(QMainWindow, Ui_MainWindow): ...@@ -820,6 +872,14 @@ class MainWindow(QMainWindow, Ui_MainWindow):
print("availableMetaData:", self.player.availableMetaData()) print("availableMetaData:", self.player.availableMetaData())
print("[openVideoFile] self.player.duration", self.player.duration()) print("[openVideoFile] self.player.duration", self.player.duration())
return path return path
def openExcelFile(self):
path = QFileDialog.getOpenFileUrl(self, "选择待导入Excel", QUrl(
os.getcwd()), "Excel Files(*.xls *xlsx *.);; 所有文件(*.*)")[0]
# print(path.url()[8:])
print(path)
print(path.url()[8:])
return path.url()[8:]
# 在初始化工程时 # 在初始化工程时
def init_ProjectContext_VideoPath(self, path: str): def init_ProjectContext_VideoPath(self, path: str):
...@@ -1435,6 +1495,8 @@ class MainWindow(QMainWindow, Ui_MainWindow): ...@@ -1435,6 +1495,8 @@ class MainWindow(QMainWindow, Ui_MainWindow):
print("self.player.position()", self.player.position()) print("self.player.position()", self.player.position())
cur_time = round(self.player.position()/1000, 3) cur_time = round(self.player.position()/1000, 3)
idx = self.calculate_element_row(cur_time) idx = self.calculate_element_row(cur_time)
print(">>>>>>>>>>>>>>>>>>>>>>>>>add row")
print("idex :" + str(idx))
print("[insert_aside_from_now_slot] idx=", idx) print("[insert_aside_from_now_slot] idx=", idx)
# 其实end_time目前是没啥用的,可以删掉了 # 其实end_time目前是没啥用的,可以删掉了
print("cur_lens", len(self.projectContext.all_elements)) print("cur_lens", len(self.projectContext.all_elements))
...@@ -1458,6 +1520,33 @@ class MainWindow(QMainWindow, Ui_MainWindow): ...@@ -1458,6 +1520,33 @@ class MainWindow(QMainWindow, Ui_MainWindow):
break break
idx += 1 idx += 1
return idx return idx
def removeByCurTime(self, cur_time, aside):
"""确认表格中适合cur_time的插入位置
Args:
cur_time (float): 想要插入旁白的当前时间
Returns:
int: 合适位置所在的行号
"""
idx = 0
same_flag = False
while idx < len(self.projectContext.all_elements):
# 过滤旁白
if self.projectContext.all_elements[idx].suggest != None and self.projectContext.all_elements[idx].suggest != "":
if float(cur_time) == float(self.projectContext.all_elements[idx].st_time_sec):
if aside != self.projectContext.all_elements[idx].aside:
print("移除旧的,再新增")
self.projectContext.all_elements.pop(idx)
else:
same_flag = True
break
if float(cur_time) < float(self.projectContext.all_elements[idx].st_time_sec):
print(">>>>>>>>>bbbbbbbb")
break
idx += 1
return idx,same_flag
def add_line_operation_slot(self, row: int, start_time: str, end_time: str, subtitle: str, suggest: str, aside: str, speed: str): def add_line_operation_slot(self, row: int, start_time: str, end_time: str, subtitle: str, suggest: str, aside: str, speed: str):
"""添加一行信息 """添加一行信息
...@@ -1495,6 +1584,63 @@ class MainWindow(QMainWindow, Ui_MainWindow): ...@@ -1495,6 +1584,63 @@ class MainWindow(QMainWindow, Ui_MainWindow):
self.refresh_tab_slot() self.refresh_tab_slot()
self.prompt_dialog.show_with_msg("操作成功!!请查看变化") self.prompt_dialog.show_with_msg("操作成功!!请查看变化")
def batch_add_line_operation_slot(self, elements):
"""添加一行信息
Args:
row (int): 在该行后添加信息
start_time (str): 起始时间
end_time (str): 终止时间
subtitle (str): 字幕文本
suggest (str): 建议
aside (str): 旁白文本
speed (str): 旁白语速
"""
# 注意,这里需要用同一对象,不能生成多个Element
for new_element in elements:
start_time = new_element.st_time_sec
suggest = new_element.suggest
aside = new_element.aside
subtitle = new_element.subtitle
# insert_time = self.getSeconds(start_time)
# cur_time = round(insert_time/1000, 3)
row, same_flag = self.removeByCurTime(start_time, aside)
#该旁白没有被改动
if same_flag:
continue
# row = self.calculate_element_row(cur_time)
self.projectContext.all_elements.insert(int(row), new_element)
self.all_tableWidget_idx = int(row)
if suggest is not None and "插入旁白,推荐字数为" in suggest:
idx = 0
while idx < len(self.projectContext.aside_list):
if float(start_time) == float(self.projectContext.aside_list[idx].st_time_sec) and aside != self.projectContext.aside_list[idx].aside:
self.projectContext.aside_list.pop(idx)
break
if float(start_time) < float(self.projectContext.aside_list[idx].st_time_sec):
break
idx += 1
self.projectContext.aside_list.insert(idx, new_element)
self.pb_tableWidget_idx = idx
self.do_generate_audio_by_aside_row(idx)
else:
idx = 0
while idx < len(self.projectContext.subtitle_list):
if float(start_time) == float(self.projectContext.subtitle_list[idx].st_time_sec) and subtitle != self.projectContext.subtitle_list[idx].subtitle:
self.projectContext.subtitle_list.pop(idx)
break
if float(start_time) < float(self.projectContext.subtitle_list[idx].st_time_sec):
break
idx += 1
self.projectContext.subtitle_list.insert(idx, new_element)
self.zm_tableWidget_idx = idx
self.refresh_tab_slot()
self.prompt_dialog.show_with_msg("操作成功!!请查看变化")
def getSeconds(self, time_str):
hours, minutes = map(int, time_str.split('.'))
seconds = hours * 3600 + minutes * 60
return seconds
def mod_line_operation_slot(self, row: int, start_time: str, end_time: str, subtitle: str, suggest: str, aside: str, speed: str): def mod_line_operation_slot(self, row: int, start_time: str, end_time: str, subtitle: str, suggest: str, aside: str, speed: str):
"""修改一行 """修改一行
......
...@@ -603,6 +603,7 @@ ...@@ -603,6 +603,7 @@
</property> </property>
<addaction name="action_3"/> <addaction name="action_3"/>
<addaction name="action_4"/> <addaction name="action_4"/>
<addaction name="action_5"/>
<addaction name="separator"/> <addaction name="separator"/>
<addaction name="action_refresh_tab"/> <addaction name="action_refresh_tab"/>
</widget> </widget>
...@@ -656,6 +657,11 @@ ...@@ -656,6 +657,11 @@
<string>旁白音频合成</string> <string>旁白音频合成</string>
</property> </property>
</action> </action>
<action name="action_5">
<property name="text">
<string>旁白导入</string>
</property>
</action>
<action name="action_refresh_tab"> <action name="action_refresh_tab">
<property name="text"> <property name="text">
<string>刷新且保存表格</string> <string>刷新且保存表格</string>
......
...@@ -359,6 +359,8 @@ class Ui_MainWindow(object): ...@@ -359,6 +359,8 @@ class Ui_MainWindow(object):
self.action_3.setObjectName("action_3") self.action_3.setObjectName("action_3")
self.action_4 = QtWidgets.QAction(MainWindow) self.action_4 = QtWidgets.QAction(MainWindow)
self.action_4.setObjectName("action_4") self.action_4.setObjectName("action_4")
self.action_5 = QtWidgets.QAction(MainWindow)
self.action_5.setObjectName("action_5")
self.action_operate = QtWidgets.QAction(MainWindow) self.action_operate = QtWidgets.QAction(MainWindow)
self.action_operate.setObjectName("action_operate") self.action_operate.setObjectName("action_operate")
self.action_export = QtWidgets.QAction(MainWindow) self.action_export = QtWidgets.QAction(MainWindow)
...@@ -384,6 +386,7 @@ class Ui_MainWindow(object): ...@@ -384,6 +386,7 @@ class Ui_MainWindow(object):
self.menu_2.addAction(self.action_operate) self.menu_2.addAction(self.action_operate)
self.menu_3.addAction(self.action_3) self.menu_3.addAction(self.action_3)
self.menu_3.addAction(self.action_4) self.menu_3.addAction(self.action_4)
self.menu_3.addAction(self.action_5)
self.menu_3.addSeparator() self.menu_3.addSeparator()
self.menubar.addAction(self.menu.menuAction()) self.menubar.addAction(self.menu.menuAction())
self.menubar.addAction(self.menu_2.menuAction()) self.menubar.addAction(self.menu_2.menuAction())
...@@ -417,6 +420,7 @@ class Ui_MainWindow(object): ...@@ -417,6 +420,7 @@ class Ui_MainWindow(object):
self.action_redo.setText(_translate("MainWindow", "重做")) self.action_redo.setText(_translate("MainWindow", "重做"))
self.action_3.setText(_translate("MainWindow", "旁白区间检测")) self.action_3.setText(_translate("MainWindow", "旁白区间检测"))
self.action_4.setText(_translate("MainWindow", "旁白音频合成")) self.action_4.setText(_translate("MainWindow", "旁白音频合成"))
self.action_5.setText(_translate("MainWindow", "旁白导入"))
self.action_operate.setText(_translate("MainWindow", "操作表格")) self.action_operate.setText(_translate("MainWindow", "操作表格"))
self.action_export.setText(_translate("MainWindow", "导出")) self.action_export.setText(_translate("MainWindow", "导出"))
self.action_insert_aside_from_now.setText(_translate("MainWindow", "当前位置插入旁白")) self.action_insert_aside_from_now.setText(_translate("MainWindow", "当前位置插入旁白"))
......
...@@ -8,6 +8,7 @@ from PyQt5.QtWidgets import *; ...@@ -8,6 +8,7 @@ from PyQt5.QtWidgets import *;
from main_window import MainWindow from main_window import MainWindow
from qt_material import apply_stylesheet from qt_material import apply_stylesheet
import os import os
import subprocess
os.environ['PYQTGRAPH_QT_LIB'] = 'PyQt5' os.environ['PYQTGRAPH_QT_LIB'] = 'PyQt5'
project_path = None project_path = None
...@@ -22,6 +23,7 @@ def change_project_path(path): ...@@ -22,6 +23,7 @@ def change_project_path(path):
if __name__ == '__main__': if __name__ == '__main__':
try: try:
subprocess.call(['deploy.bat'])
QCoreApplication.setAttribute(Qt.AA_EnableHighDpiScaling) QCoreApplication.setAttribute(Qt.AA_EnableHighDpiScaling)
QCoreApplication.setAttribute(Qt.AA_UseHighDpiPixmaps) QCoreApplication.setAttribute(Qt.AA_UseHighDpiPixmaps)
currentExitCode = MainWindow.EXIT_CODE_REBOOT currentExitCode = MainWindow.EXIT_CODE_REBOOT
......
...@@ -632,6 +632,7 @@ ...@@ -632,6 +632,7 @@
</property> </property>
<addaction name="action_3"/> <addaction name="action_3"/>
<addaction name="action_4"/> <addaction name="action_4"/>
<addaction name="action_5"/>
<addaction name="separator"/> <addaction name="separator"/>
<addaction name="action_refresh_tab"/> <addaction name="action_refresh_tab"/>
</widget> </widget>
...@@ -685,6 +686,11 @@ ...@@ -685,6 +686,11 @@
<string>旁白音频合成</string> <string>旁白音频合成</string>
</property> </property>
</action> </action>
<action name="action_5">
<property name="text">
<string>旁白导入</string>
</property>
</action>
<action name="action_view_history"> <action name="action_view_history">
<property name="text"> <property name="text">
<string>----</string> <string>----</string>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment