123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 |
- 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()
-
|