お世話になっております.
wordファイルを自動翻訳処理したく,下記サイトを参考に コードを組んだ コピペしたのですが,array.extend([ori_text, translation.text])
の部分でエラーします.
ワードファイルを読み込んで自動翻訳し,新規ドキュメントファイルとして出力するものです.
参考にしたサイト様:
Python|フォルダ内のワード(Word)を日本語から英語に機械翻訳するツール
環境
Windows10
python3.8
VScode1.47
###メインスクリプト
python
1import win32com.client 2import os 3import pathlib 4import sys 5from googletrans import Translator 6 7 8def main(): 9 Application = win32com.client.Dispatch("Word.Application") 10 Application.Visible = True 11 12 os.chdir(os.path.dirname(__file__)) 13 path = os.getcwd() 14 folder = path + "\Translate" 15 WordFolder = pathlib.Path(folder) 16 Filepath=[ str(p) for p in WordFolder.iterdir()] 17 Filename=[p.name for p in WordFolder.iterdir() if p.is_file()] 18 19 for k in range(1, len(Filepath)+1): 20 GetTranslation(Application, Filepath[k-1] , Filename[k-1] , folder) 21 22 23def GetTranslation(Application, Filepath, Filename, folder): 24 doc = Application.Documents.Open(Filepath) 25 cmax= doc.paragraphs.count 26 27 array=[] 28 translator = Translator() 29 for i in reversed(range(1, cmax+1)): 30 rtext=doc.paragraphs(i).Range.Text 31 rtext=rtext.replace("\r","") 32 ori_text=rtext.replace("\x07","") 33 print(i) 34 if str(ori_text)=="": 35 z=1 36 else: 37 z=0 38 translation = translator.translate(ori_text, dest='en') 39 array.extend([ori_text, translation.text]) ##ここでエラーする 40 41 if z == 0: 42 try: 43 doc.Paragraphs(i).Range.InsertAfter ("\n" + translation.text) 44 except: 45 print(i, "error") 46 47 newFilePath=folder + "\translated_" + Filename 48 print(Filename) 49 doc.SaveAs2(newFilePath) 50 doc.Close() 51 52if __name__ == "__main__": 53 main()
###エラー文
ユーザ名まわりは編集しています..
Traceback (most recent call last): File "%USERPROFILE%\AppData\Local\Programs\Python\Python38\lib\runpy.py", line 194, in _run_module_as_main return _run_code(code, main_globals, None, File "%USERPROFILE%\AppData\Local\Programs\Python\Python38\lib\runpy.py", line 87, in _run_code exec(code, run_globals) File "%USERPROFILE%.vscode\extensions\ms-python.python-2020.6.91350\pythonFiles\lib\python\debugpy\__main__.py", line 45, in <module> cli.main() File "%USERPROFILE%.vscode\extensions\ms-python.python-2020.6.91350\pythonFiles\lib\python\debugpy/..\debugpy\server\cli.py", line 430, in main run() File "%USERPROFILE%.vscode\extensions\ms-python.python-2020.6.91350\pythonFiles\lib\python\debugpy/..\debugpy\server\cli.py", line 267, in run_file runpy.run_path(options.target, run_name=compat.force_str("__main__")) File "%USERPROFILE%\AppData\Local\Programs\Python\Python38\lib\runpy.py", line 265, in run_path return _run_module_code(code, init_globals, run_name, File "%USERPROFILE%\AppData\Local\Programs\Python\Python38\lib\runpy.py", line 97, in _run_module_code _run_code(code, mod_globals, init_globals, File "%USERPROFILE%\AppData\Local\Programs\Python\Python38\lib\runpy.py", line 87, in _run_code exec(code, run_globals) File "%USERPROFILE%\Desktop\develop\Auto-Translate\translate.py", line 52, in <module> main() File "%USERPROFILE%\Desktop\develop\Auto-Translate\translate.py", line 20, in main GetTranslation(Application, Filepath[k-1] , Filename[k-1] , folder) File "%USERPROFILE%\Desktop\develop\Auto-Translate\translate.py", line 38, in GetTranslation array.extend([ori_text, translation.text]) UnboundLocalError: local variable 'translation' referenced before assignment
一行前で宣言してるやんけ,,,とサッパリでして,ここに時間を割くよりは細かい仕様変更に時間を費やしたいと思い投稿いたしました.
初歩的な内容かと思いますが,何卒よろしくお願い申し上げます.
###補足
aokikenichi様,のご回答を受けてarray.extend([ori_text, translation.text])
をif分岐の前に移動し,実行したところ下記のエラーが出ました.
%USERPROFILE%>%USERPROFILE%/AppData/Local/Programs/Python/Python38/python.exe %USERPROFILE%/Desktop/develop/Auto-Translate/translate.py 829 Traceback (most recent call last): File "%USERPROFILE%/Desktop/develop/Auto-Translate/translate.py", line 53, in <module> main() File "%USERPROFILE%/Desktop/develop/Auto-Translate/translate.py", line 20, in main GetTranslation(Application, Filepath[k-1] , Filename[k-1] , folder) File "%USERPROFILE%/Desktop/develop/Auto-Translate/translate.py", line 34, in GetTranslation translation = translator.translate(ori_text, dest='en') File "%USERPROFILE%\AppData\Local\Programs\Python\Python38\lib\site-packages\googletrans\client.py", line 182, in translate data = self._translate(text, dest, src, kwargs) File "%USERPROFILE%\AppData\Local\Programs\Python\Python38\lib\site-packages\googletrans\client.py", line 78, in _translate token = self.token_acquirer.do(text) File "%USERPROFILE%\AppData\Local\Programs\Python\Python38\lib\site-packages\googletrans\gtoken.py", line 194, in do self._update() File "%USERPROFILE%\AppData\Local\Programs\Python\Python38\lib\site-packages\googletrans\gtoken.py", line 54, in _update r = self.client.get(self.host) File "%USERPROFILE%\AppData\Local\Programs\Python\Python38\lib\site-packages\httpx\_client.py", line 755, in get return self.request( File "%USERPROFILE%\AppData\Local\Programs\Python\Python38\lib\site-packages\httpx\_client.py", line 600, in request return self.send( File "%USERPROFILE%\AppData\Local\Programs\Python\Python38\lib\site-packages\httpx\_client.py", line 620, in send response = self.send_handling_redirects( File "%USERPROFILE%\AppData\Local\Programs\Python\Python38\lib\site-packages\httpx\_client.py", line 647, in send_handling_redirects response = self.send_handling_auth( File "%USERPROFILE%\AppData\Local\Programs\Python\Python38\lib\site-packages\httpx\_client.py", line 684, in send_handling_auth response = self.send_single_request(request, timeout) File "%USERPROFILE%\AppData\Local\Programs\Python\Python38\lib\site-packages\httpx\_client.py", line 714, in send_single_request ) = transport.request( File "%USERPROFILE%\AppData\Local\Programs\Python\Python38\lib\site-packages\httpcore\_sync\http_proxy.py", line 110, in request return self._tunnel_request( File "%USERPROFILE%\AppData\Local\Programs\Python\Python38\lib\site-packages\httpcore\_sync\http_proxy.py", line 191, in _tunnel_request proxy_response = proxy_connection.request( File "%USERPROFILE%\AppData\Local\Programs\Python\Python38\lib\site-packages\httpcore\_sync\connection.py", line 65, in request self.socket = self._open_socket(timeout) File "%USERPROFILE%\AppData\Local\Programs\Python\Python38\lib\site-packages\httpcore\_sync\connection.py", line 85, in _open_socket return self.backend.open_tcp_stream( File "%USERPROFILE%\AppData\Local\Programs\Python\Python38\lib\site-packages\httpcore\_backends\sync.py", line 139, in open_tcp_stream return SyncSocketStream(sock=sock) File "%USERPROFILE%\AppData\Local\Programs\Python\Python38\lib\contextlib.py", line 131, in __exit__ self.gen.throw(type, value, traceback) File "%USERPROFILE%\AppData\Local\Programs\Python\Python38\lib\site-packages\httpcore\_exceptions.py", line 12, in map_exceptions raise to_exc(exc) from None httpcore._exceptions.ConnectError: [Errno 0] Error
社用PCのプロキシ設定によるエラーでしょうか..?
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/07/13 00:23