Get file details in python - python

I'm trying to extract the details of a file using Python. Specifically, when I right click a photo and select properties, under the Details tab of the menu that pops up, there's a whole bunch of details about the file. What I really need is the contents of the "People" detail.
This is the menu in question:
Is there a good way of getting that People detail in a string or something?
Some people have suggested using ExifRead. I tried that, but it didn't pull the People tag out of the Exif data.

This is not EXIF data but rather data that Windows populates for different types of objects in the Windows Property System.
The one you are concerned with is called System.Photo.PeopleNames:
propertyDescription
name = System.Photo.PeopleNames
shellPKey = PKEY_Photo_PeopleNames
formatID = E8309B6E-084C-49B4-B1FC-90A80331B638
propID = 100
searchInfo
inInvertedIndex = true
isColumn = true
isColumnSparse = true
columnIndexType = OnDemand
maxSize = 128
mnemonics = people|people tag|people tags
labelInfo
label = People
sortDescription
invitationText = Add a people tag
hideLabel = false
typeInfo
type = String
groupingRange = Discrete
isInnate = true
canBePurged = true
multipleValues = true
isGroup = false
aggregationType = Union
isTreeProperty = false
isViewable = true
isQueryable (Vista) = false
includeInFullTextQuery (Vista) = false
searchRawValue (Windows 7) = true
conditionType = String
defaultOperation = Equal
aliasInfo
sortByAlias = None
additionalSortByAliases = None
displayInfo
defaultColumnWidth = 11
displayType = String
alignment = Left
relativeDescriptionType = General
defaultSortDirection = Ascending
stringFormat
formatAs = General
booleanFormat
formatAs = YesNo
numberFormat
formatAs = General
formatDurationAs = hh:mm:ss
dateTimeFormat
formatAs = General
formatTimeAs = ShortTime
formatDateAs = ShortDate
enumeratedList
defaultText
useValueForDefault = False
enum
value
text
enumRange
minValue
setValue
text
drawControl
control = Default
editControl
control = Default
filterControl
control = Default
queryControl
control = Default
To access this information in Python, use win32com.propsys.

Related

django.core.exceptions.ValidationError: ['“TRUE” value must be either True or False.']

I am trying to upload data in the django ORM by a script for which I have written this
for index, row in df.iterrows():
allocated = row['is_allocated']
delivery_required_on = row['delivery_required_on']
linked = row['linked']
raised_by = row['raised_by']
raised_for = Company.objects.get(pk=row['raised_for']) ### double check
rejected = row['is_rejected']
reason = row['reason']
remarks = row['remarks']
created = row['created_at']
owner = User.objects.get(pk=row['New owner'])
j = literal_eval(row['flows'])
flows = []
mobj = MaterialRequest.objects.create(owner=owner, is_allocated=allocated,
delivery_required_on=delivery_required_on, linked=linked,
raised_by=raised_by, raised_for=raised_for, is_rejected=rejected,
reason=reason, remarks=remarks, created=created)
It is running fine when the data is something like the following:
But as soon as is_allocated reaches False it shows the following error:
django.core.exceptions.ValidationError: ['“TRUE” value must be either
True or False.']
I am unable to find something related to this
it seems like the is_allocated property of your model is a boolean. So you should assign a boolean value to it. But your column values in the data frame are strings TRUE and FALSE.
replacing this line
allocated = row['is_allocated']
with
allocated = (row['is_allocated'] == 'TRUE')
might help.
if you have None other than True and False values, consider that too.
It is because you are trying to store a string in a boolean field. One solution is to change your string type to boolean. Maybe having a function like following in your code solves your problem:
def to_boolean(raw_value: str) -> bool:
if not isinstance(raw_value, str):
raw_value = str(raw_value)
raw_value = raw_value.strip()
return {'true': True, 'false': False}.get(raw_value.lower(), False)
and then use it in your loop like (where ever you think your field type is boolean):
for index, row in df.iterrows():
allocated = to_boolean(row['is_allocated'])
delivery_required_on = row['delivery_required_on']
linked = row['linked']
raised_by = row['raised_by']
raised_for = Company.objects.get(pk=row['raised_for']) ### double check
rejected = to_boolean(row['is_rejected'])
reason = row['reason']
remarks = row['remarks']
created = row['created_at']
owner = User.objects.get(pk=row['New owner'])
j = literal_eval(row['flows'])
flows = []
mobj = MaterialRequest.objects.create(owner=owner, is_allocated=allocated,
delivery_required_on=delivery_required_on, linked=linked,
raised_by=raised_by, raised_for=raised_for, is_rejected=rejected,
reason=reason, remarks=remarks, created=created)

how to add rule to auto change strings to use double quotes with yapf formatter and add comma after the last item of the list?

In the doc don't have a list available properties, and i find one file with some properties in gist.github, but i don't find the key for this rules.
Example of my problem:
I have this code:
endpoints = [
path('check_email/', check_email)
]
and i want that this code will be formated to:
endpoints = [
path("check_email/", check_email),
]
I have setup.cfg file with some settings:
[yapf]
# custom
based_on_style = google
split_before_logical_operator = True
align_closing_bracket_with_visual_indent = True
allow_multiline_dictionary_keys = True
allow_multiline_lambdas = True
blank_line_before_nested_class_or_def = True
indent_dictionary_value = True
dedent_closing_brackets = True
no_spaces_around_selected_binary_operators = set()
split_before_expression_after_opening_paren = True
split_arguments_when_comma_terminated = True
split_complex_comprehension = True

How to insert a Libreoffice writer Annotation with a date using Python

Please note, this is a self answered question for reference.
Most of the references to com.sun.star.text.textfield.Annotation refer to Date as being a :: com :: sun :: star :: util :: reference but no end of fiddling about with the contents will actually create an annotation with a date.
Setting Date.Year, Date.Month and Date.Day will appear successful but the annotation itself still appears without a date i.e.
anno = model.createInstance("com.sun.star.text.textfield.Annotation")
anno.Content = "this is my annotation/comment"
anno.Author = doc.DocumentProperties.Author
anno.Date.Year = 2020
anno.Date.Month = 5
anno.Date.Day = 18
The documentation is not always complete, or is not obvious, depending upon where you look.
For LibreOffice 6.0 https://api.libreoffice.org/docs/idl/ref/Annotation_8idl_source.html
The Annotation.idl is described as:
service com::sun::star::text::TextField;
[property]string Author;
[optional, property]string Initials;
[optional, property]string Name;
[property]string Content;
[property]com::sun::star::util::Date Date;
[optional, property]com::sun::star::util::DateTime DateTimeValue;
The key here is the optional DateTimeValue which it would appear is the item that needs to be set to provide the Annotation with a Date and Time.
DateTimeValue structure is from com.sun.star.util.DateTime
To create an annotation (with a date and time) in a writer document using a python script, use the following as a template.
from uno import createUnoStruct
import time
def fs_Annotation(*args):
#get the doc from the scripting context which is made available to all scripts
desktop = XSCRIPTCONTEXT.getDesktop()
model = desktop.getCurrentComponent()
try:
text = model.Text
except:
# The macro has been called externally but LibreOffice was not running at the time
return None
tRange = text.End
cursor = desktop.getCurrentComponent().getCurrentController().getViewCursor()
doc = XSCRIPTCONTEXT.getDocument()
# you cannot insert simple text and text into a table with the same method
# so we have to know if we are in a table or not.
# oTable and oCurCell will be null if we are not in a table
oTable = cursor.TextTable
oCurCell = cursor.Cell
anno = model.createInstance("com.sun.star.text.textfield.Annotation")
anno.Content = "this is my annotation/comment"
#Use documents author
#anno.Author = doc.DocumentProperties.Author
#Use hardcoded text
anno.Author = "Joe Bloggs"
t = time.localtime()
dtv=createUnoStruct("com.sun.star.util.DateTime")
dtv.Year = t.tm_year
dtv.Month = t.tm_mon
dtv.Day = t.tm_mday
dtv.Hours = t.tm_hour
dtv.Minutes= t.tm_min
dtv.Seconds = t.tm_sec
dtv.NanoSeconds = 0
anno.DateTimeValue = dtv
if oCurCell == None: # Inserting into text
text.insertTextContent(cursor, anno, True)
else: # Inserting into a table
oCurCell.insertTextContent(cursor, anno, False)
return None
Section 7.7.2 of Andrew's macro document gives the following, although I have not tested it.
Sub AddNoteAtCursor
Dim vDoc, vViewCursor, oCurs, vTextField
Dim s$
'Lets lie and say that this was added ten days ago!'
Dim aDate As New com.sun.star.util.Date
With aDate
.Day = Day(Now - 10)
.Month = Month(Now - 10)
.Year = Year(Now - 10)
End With
vDoc = ThisComponent
vViewCursor = vDoc.getCurrentController().getViewCursor()
oCurs=vDoc.getText().createTextCursorByRange(vViewCursor.getStart())
s = "com.sun.star.text.TextField.Annotation"
vTextField = vDoc.createInstance(s)
With vTextField
.Author = "AP"
.Content = "It sure is fun to insert notes into my document"
'Ommit the date and it defaults to today!'
.Date = aDate
End With
vDoc.Text.insertTextContent(oCurs, vTextField, False)
End Sub
The API docs contain the same information as the IDL file but are somewhat easier to read.
https://www.openoffice.org/api/docs/common/ref/com/sun/star/text/textfield/Annotation.html

npyscreen - addVisibleWhenSelected - Clearing value when hidden

I'm quite new to python and I'm trying to put together a little app with npyscreen. Part of this app has a page with a FormControlBox, that reveals a FormControlBox when selected. This second FormControlBox, when selected, reveals some text fields.
The issue I'm having is getting the second FormControlBox to change its value to False if the first one is unselected (making the second hidden). Here is the approach I'm attempting to take in this form class.
class envMenuForm(npyscreen.Form):
def afterEditing(self):
# Updating mapping with values set from form creation
hostConfig["hostname"] = self.hostname.value
hostConfig["domain"] = self.domain.value
hostConfig["fqdn"] = self.fqdn.value
self.parentApp.setNextForm('CEV')
def create(self):
# Defining vars from current baseConfig mapping from JSON file
self.hostname = hostConfig["hostname"]
self.domain = hostConfig["domain"]
self.fqdn = hostConfig["fqdn"]
# Adding text fields with the defaults from the config file
self.hostname = self.add(npyscreen.TitleText, name = "System Hostname:", value = self.hostname)
self.domain = self.add(npyscreen.TitleText, name = "System Domain:", value = self.domain)
self.fqdn = self.add(npyscreen.TitleText, name = "System Fully Qualified Domain Name:", value = self.fqdn)
self.et0status = self.add(npyscreen.FormControlCheckbox, name="Enable Eth0", value = False)
self.et0type = self.add(npyscreen.FormControlCheckbox, name = "Configure as Static Address (ignore for DHCP)", value = False)
self.et0ipaddress = self.add(npyscreen.TitleText, name = "IP Address", value = "127.0.0.1")
self.et0status.addVisibleWhenSelected(self.et0type)
self.et0type.addVisibleWhenSelected(self.et0ipaddress)
def while_editing(self,arg):
if arg is self.et0type:
if arg.hidden:
self.et0type.value = False
After a bunch of refactoring I was able to solve this utilizing the adjust_widgets() with some background logic to ensure it doesn't fire off too much.

Working with Python and pickle to save complex data in object

I am experimenting with python to do a script for a program that works with python, and I need to save an object (with custom classes and arrays inside) to a file so that I can read it afterwards (so that I don't have to remake the object everytime, which takes hours)
I was reading in many forums that the easiest way to do that is to use pickle, but I am making a mistake in some place and I don't understand where...
Now, the code would be:
First I define this class:
class Issue_class:
Title_ID = None
Publisher_ID = None
Imprint_ID = None
Volume = None
Format = None
Color = None
Original = None
Rating = None
Issue_Date_Month = None
Issue_Date_Year = None
Reprint = None
Pages = None
Issue_Title = None
Number = None
Number_str = None
Synopsis = None
Characters_ID = None
Groups_ID = None
Writer_ID = None
Inker_ID = None
Colorist_ID = None
Letterer_ID = None
CoverArtist_ID = None
Penciller_ID = None
Editor_ID = None
Alternatives_ID = None
Reprints_ID = None
Story_ID = None
Multi = None
Multistories = None
then I define a list/array for this class:
Issuesdata = []
then during a loop I fill and append these to the list:
Issuedata = Issue_class()
Issuedata.Color = "unknown"
Issuedata.Tagline = "none"
Issuedata.Synopsis = "none"
Issuedata.Format = "none"
Issuedata.Publisher_ID = "none"
Issuedata.Imprint_ID = -1
Issuedata.Title_ID = -1
Issuedata.Volume = "none"
Issuedata.Number = -1
Issuedata.Number_str = "none"
Issuedata.Issue_Title = "none"
Issuedata.Rating = -1
Issuedata.Pages = -1
Issuedata.Issue_Date_Year = 0
Issuedata.Issue_Date_Month = 0
Issuedata.Original = True
Issuedata.Reprint = False
Issuedata.Multi= True
Issuedata.Letterer_ID = []
Issuedata.Characters_ID = []
Issuedata.Story_ID = []
Issuedata.Groups_ID = []
Issuedata.Writer_ID = []
Issuedata.Penciller_ID = []
Issuedata.Alternatives_ID = []
Issuedata.Reprints_ID = []
Issuedata.Inker_ID = []
Issuedata.Colorist_ID = []
Issuedata.Editor_ID = []
Issuedata.CoverArtist_ID = []
Issuedata.Multistories = []
Then I work with the data inside the object, and when it is complete, I append it to the list:
Issuesdata.append(Issuedata)
After that I print some info inside one of the objects in the list to be sure everything is ok:
print Issuesdata[3].Title_ID
print Issuesdata[3].Publisher_ID
print Issuesdata[3].Imprint_ID
print Issuesdata[3].Volume
print Issuesdata[3].Format
etc...
And everything is ok, the printed data is perfect
Now, I try to save the list to a file with:
filehandler = open("data.dat","wb")
pickle.dump(Issuesdata,filehandler)
filehandler.close()
This create the file with info inside... but when I try to read it with:
file = open("data.dat",'rb')
Issuesdat = pickle.load(file)
file.close()
The Python console tells me "'module' object has no attribute 'Issue_class'"
The first thing I thought was that I was reading the file wrong... But then I open the saved file with notepad and inside it it was full of "wrong data", like name of files or name of classes outside the code... which makes me suspect I am dumping the data wrong in the file...
Am I using pickle wrong?
Ok, I found the problem... It seems you have to define the class of your object in the main module for pickle to see it... I had it defined in the module I was working and calling the pickle command...
Try using pandas library with simple functions like:
DataFrame.to_pickle(file-path) to save pandas Dataframe in pickle.
pandas.read_pickle(file-path) to read pickle file.
Here you can find pandas reference to_pickle read_pickle.

Categories

Resources