Skip to content

Instantly share code, notes, and snippets.

@mkckr0
Created February 3, 2023 02:35
Show Gist options
  • Save mkckr0/09dc0deb93a2837fd5ff583238299f1c to your computer and use it in GitHub Desktop.
Save mkckr0/09dc0deb93a2837fd5ff583238299f1c to your computer and use it in GitHub Desktop.
Python access Windows Property System
from datetime import datetime
import os
from pathlib import PurePath
from urllib.parse import urlparse
from win32comext.shell import shellcon
from win32comext.propsys import propsys, pscon
import pythoncom
def set_media_time(path: str, time: str):
# pythoncom.CoInitialize()
path = os.path.abspath(path)
# print(time)
ext = PurePath(path).suffix
if ext == '.mp3':
os.utime(path, (os.stat(path).st_atime, datetime.fromisoformat(time).timestamp()))
else:
try:
pps = propsys.SHGetPropertyStoreFromParsingName(path, None, shellcon.GPS_READWRITE)
if ext == '.jpg':
pps.SetValue(pscon.PKEY_Photo_DateTaken, propsys.PROPVARIANTType(time))
else:
pps.SetValue(pscon.PKEY_Media_DateEncoded, propsys.PROPVARIANTType(time))
pps.Commit()
except Exception as e:
print(e)
def set_media_title(path: str, title: str):
path = os.path.abspath(path)
# print(time)
# ext = PurePath(path).suffix
try:
pps = propsys.SHGetPropertyStoreFromParsingName(path, None, shellcon.GPS_READWRITE)
pps.SetValue(pscon.PKEY_Title, propsys.PROPVARIANTType(title))
pps.Commit()
except Exception as e:
print(e)
def get_media_title(path: str):
path = os.path.abspath(path)
try:
pps = propsys.SHGetPropertyStoreFromParsingName(path, None, shellcon.GPS_DEFAULT)
return pps.GetValue(pscon.PKEY_Title).GetValue()
except Exception as e:
print(e)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment