|
@@ -5,8 +5,22 @@ import shutil
|
|
|
import os
|
|
|
import chardet
|
|
|
import zipfile
|
|
|
-from test_chardet import guess_codec
|
|
|
from io import BytesIO
|
|
|
+from translate import Translator
|
|
|
+from chardet.universaldetector import UniversalDetector
|
|
|
+
|
|
|
+DEFAULT_ENCODING = "utf-8"
|
|
|
+
|
|
|
+def guess_codec(filenames: list) -> str:
|
|
|
+ codec_detector = UniversalDetector()
|
|
|
+ for filename in filenames:
|
|
|
+ codec_detector.feed(filename.encode('cp437'))
|
|
|
+ if codec_detector.done:
|
|
|
+ break
|
|
|
+
|
|
|
+ result = codec_detector.close()
|
|
|
+ encoding = result.get("encoding")
|
|
|
+ return encoding or DEFAULT_ENCODING
|
|
|
|
|
|
def check_zip(zip_filepath:str):
|
|
|
path = Path(zip_filepath)
|
|
@@ -54,5 +68,48 @@ def check_zip(zip_filepath:str):
|
|
|
n = stems.count(voice_file)
|
|
|
if n != 1:
|
|
|
raise ValueError(f"voice file is can't find is zip at scene {i+1}.")
|
|
|
+
|
|
|
+def update_zip(zip_path, lang):
|
|
|
+ temp_zip_path = zip_path + ".tmp"
|
|
|
+
|
|
|
+ with zipfile.ZipFile(zip_path, 'r') as zip_in, zipfile.ZipFile(temp_zip_path, 'w') as zip_out:
|
|
|
+ for item in zip_in.infolist():
|
|
|
+ with zip_in.open(item.filename) as src_file:
|
|
|
+ if item.filename.split('.')[-1] == "xlsx":
|
|
|
+ table = pd.read_excel(src_file, dtype=object)
|
|
|
+ table = translate_table(table, lang)
|
|
|
+ table.to_excel(Path(item.filename).name ,sheet_name='Sheet_name_1')
|
|
|
+ zip_out.write(Path(item.filename).name, item.filename)
|
|
|
+ os.remove(Path(item.filename).name)
|
|
|
+ elif item.filename.split('.')[-1] == "csv":
|
|
|
+ table = pd.read_csv(src_file, dtype=object)
|
|
|
+ table = translate_table(table, lang)
|
|
|
+ table.to_excel(Path(item.filename).name ,sheet_name='Sheet_name_1')
|
|
|
+ zip_out.write(Path(item.filename).name, item.filename)
|
|
|
+ os.remove(Path(item.filename).name)
|
|
|
+ else:
|
|
|
+ # それ以外のファイルはそのままコピー
|
|
|
+ with zip_out.open(item.filename, 'w') as dst_file:
|
|
|
+ shutil.copyfileobj(src_file, dst_file)
|
|
|
+
|
|
|
+ # 旧ZIPを削除し、新ZIPをリネーム
|
|
|
+ os.remove(zip_path)
|
|
|
+ os.rename(temp_zip_path, zip_path)
|
|
|
+
|
|
|
+def translate_table(table, lang):
|
|
|
+ translator= Translator(to_lang=lang)
|
|
|
+ print(f"translate to {lang}")
|
|
|
+ for i in range(len(table)):
|
|
|
+ if (text:=table.loc[i, ['大標']].item()):
|
|
|
+ print("大標:",text)
|
|
|
+ translation = translator.translate(text)
|
|
|
+ print("大標翻譯:",translation)
|
|
|
+ table.loc[i, ['字幕']] = translation
|
|
|
+ if (text:=table.loc[i, ['字幕']].item()):
|
|
|
+ print('字幕:',text)
|
|
|
+ translation = translator.translate(text)
|
|
|
+ print('字幕翻譯:',translation)
|
|
|
+ table.loc[i, ['字幕']] = translation
|
|
|
+ return table
|
|
|
|
|
|
|