瀏覽代碼

fix many many bugs

Alex Sidorov 1 年之前
父節點
當前提交
57eb39e61b
共有 1 個文件被更改,包括 10 次插入8 次删除
  1. 10 8
      rename_photo_from_xlsx.py

+ 10 - 8
rename_photo_from_xlsx.py

@@ -5,23 +5,22 @@ from datetime import datetime
 
 from dotenv import load_dotenv
 
-
+print("start rename_photo version 0.1")
 # Load variable from .env
 try:
     env_file = sys.argv[1]
 except IndexError:
-    env_file = os.path.join(os.path.dirname(__file__), 'environment.env')
+    env_file = './environment.env'
     
 if os.path.exists(env_file):
     load_dotenv(dotenv_path=env_file)
     
 
 prefix = F"_{os.environ.get('PREFIX')}" if os.environ.get('PREFIX') is not None else ''
-photo_to = os.path.join(os.path.dirname(__file__), str(datetime.timestamp(datetime.now())))
+photo_to = os.environ.get('PHOTO_TO') if os.environ.get('PHOTO_TO') else os.path.join(os.path.dirname(__file__), str(datetime.timestamp(datetime.now())))
     
 def check_config():
-    print(os.path.exists(os.environ.get('FILE')))
-    if os.environ.get('PHOTO_FROM') is None or not os.path.exists(os.environ.get('PHOTO_FROM')):
+    if os.environ.get('PHOTO_FROM') is None or not os.path.exists(os.path.join(os.environ.get('PHOTO_FROM').replace('\\', '/'))):
         raise FileNotFoundError("Не указана директория с фото") 
     
     if os.environ.get('FILE') is None or not os.path.exists(os.environ.get('FILE')):
@@ -30,7 +29,7 @@ def check_config():
         
 def get_list_photo() -> list[str]:
     list_files = []
-    for address, _, files in os.walk(os.environ.get('PHOTO_FROM')):
+    for address, _, files in os.walk(os.path.join(os.environ.get('PHOTO_FROM').replace('\\', '/'))):
         for file in files:
             list_files.append(os.path.join(address, file))
             
@@ -47,7 +46,7 @@ def main():
         worksheet = excel_data.active
         
         for row in range(1, worksheet.max_row+1):
-            column_tabn, column_fio  = (1, 2) if os.environ.get('SWAP') else (2, 1)   
+            column_tabn, column_fio  = (1, 2) if os.environ.get('SWAP') not in 'False' else (2, 1)   
             tabn = worksheet.cell(row=row, column=column_tabn).value
             fio = worksheet.cell(row=row, column=column_fio).value
             
@@ -67,6 +66,9 @@ def main():
         raise openpyxl.utils.exceptions.InvalidFileException('Не правильный формат, поддерживаемые форматы: .xlsx, .xlsm, .xltx, .xltm')
 
 if __name__ == "__main__":
-    main()
+    try:
+        main()
+    except KeyboardInterrupt:
+        print("bye bye")