robertvari

excel example

Jan 19th, 2020
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.72 KB | None | 0 0
  1. import  os, json, time, openpyxl
  2. from PIL import Image, ExifTags
  3.  
  4. folder_path = input("Kérlek add meg a kilistázandó mappát!")
  5.  
  6.  
  7. file_list = [ os.path.join( folder_path, f ) for f in os.listdir(folder_path) if os.path.isfile( os.path.join( folder_path, f ) ) ]
  8.  
  9.  
  10. file_data = {}
  11. for f in file_list:
  12.     file_name = os.path.basename(f)
  13.     file_size = os.path.getsize(f)
  14.     file_date = time.ctime(os.path.getmtime(f))
  15.  
  16.     file_data[f] = {"name":file_name, "size":int(file_size/1024), "creation_date":file_date }
  17.     get_folder = f"{file_data}"
  18.  
  19.     # print(file_name, file_size/1024, "KB",file_date)
  20.  
  21. with open('my_text.txt', 'a') as f:
  22.     f.write(get_folder)
  23.  
  24.  
  25. with open("file_data.json", "w") as f:
  26.     json.dump(file_data, f)
  27.  
  28. workbook = openpyxl.Workbook()
  29.  
  30. sheet = workbook.active
  31.  
  32. sheet.column_dimensions["A"].width = 30
  33. sheet.column_dimensions["B"].width = 30
  34. sheet.column_dimensions["C"].width = 30
  35.  
  36. sheet[f'A1'] = "File"
  37. sheet[f'B1'] = "Méret"
  38. sheet[f'C1'] = "Dátum"
  39.  
  40. for index, f in enumerate( file_list ):
  41.     file_name = os.path.basename(f)
  42.     file_size = os.path.getsize(f)
  43.     file_date = time.ctime(os.path.getmtime(f))
  44.  
  45.     sheet[f'A{index + 3}'] = file_name
  46.     sheet[f'B{index + 3}'] = file_size
  47.     sheet[f'c{index + 3}'] = file_date
  48.  
  49.     img = Image.open(f)
  50.     exif_data = img._getexif()
  51.  
  52.     if not exif_data:
  53.         print(f"WARNING: No exif data for {f}")
  54.         continue
  55.  
  56.     for k,v in exif_data.items():
  57.  
  58.         exif_name = ExifTags.TAGS.get(k)
  59.  
  60.         if exif_name and exif_name == "DateTime":
  61.             sheet[f'C{index + 3}'] = v
  62.  
  63.  
  64. workbook.save(filename="file_data.xlsx")
  65.  
  66. print( "A mappa",len( os.listdir(folder_path) ),"fájlt tartalmaz!" )
  67. time.sleep(5)
Advertisement
Add Comment
Please, Sign In to add comment