Ver Fonte

first commit

Alex Sidorov há 1 ano atrás
commit
a32d98f541
4 ficheiros alterados com 80 adições e 0 exclusões
  1. 1 0
      .gitignore
  2. 5 0
      environment.env
  3. 72 0
      rename_photo_from_xlsx.py
  4. 2 0
      requirements.txt

+ 1 - 0
.gitignore

@@ -0,0 +1 @@
+.env

+ 5 - 0
environment.env

@@ -0,0 +1,5 @@
+FILE=./name_files.xlsx
+PHOTO_FROM=/home/asidorov/Документы/Фотографии ПК 2023
+PHOTO_TO=./photo
+PREFIX=1куср
+SWAP=Fasle

+ 72 - 0
rename_photo_from_xlsx.py

@@ -0,0 +1,72 @@
+import openpyxl
+import os, sys
+from shutil import copyfile
+from datetime import datetime
+
+from dotenv import load_dotenv
+
+
+# Load variable from .env
+try:
+    env_file = sys.argv[1]
+except IndexError:
+    env_file = os.path.join(os.path.dirname(__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())))
+    
+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')):
+        raise FileNotFoundError("Не указана директория с фото") 
+    
+    if os.environ.get('FILE') is None or not os.path.exists(os.environ.get('FILE')):
+        raise FileNotFoundError('Не указана файл с данными')
+        
+        
+def get_list_photo() -> list[str]:
+    list_files = []
+    for address, _, files in os.walk(os.environ.get('PHOTO_FROM')):
+        for file in files:
+            list_files.append(os.path.join(address, file))
+            
+    return list_files
+    
+def main():
+    check_config()
+    
+    if not os.path.exists(photo_to):
+        os.mkdir(photo_to)
+    
+    try:
+        excel_data = openpyxl.load_workbook(os.environ.get('FILE'))
+        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)   
+            tabn = worksheet.cell(row=row, column=column_tabn).value
+            fio = worksheet.cell(row=row, column=column_fio).value
+            
+            try:
+                file = list(filter(lambda x: str(int(tabn)) in x, get_list_photo()))[0]
+
+                print(os.path.join(photo_to, F"{fio}{prefix}.JPG"))
+                copyfile(file, os.path.join(photo_to, F"{fio}{prefix}.JPG"))
+            except ValueError:
+                pass
+            except TypeError:
+                pass
+            except IndexError:
+                pass
+            
+    except openpyxl.utils.exceptions.InvalidFileException:
+        raise openpyxl.utils.exceptions.InvalidFileException('Не правильный формат, поддерживаемые форматы: .xlsx, .xlsm, .xltx, .xltm')
+
+if __name__ == "__main__":
+    main()
+
+    

+ 2 - 0
requirements.txt

@@ -0,0 +1,2 @@
+openpyxl==3.1.2
+python-dotenv==1.0.0