diff --git a/pyOneNote/FileNode.py b/pyOneNote/FileNode.py index a5f04d0..c890922 100644 --- a/pyOneNote/FileNode.py +++ b/pyOneNote/FileNode.py @@ -552,7 +552,12 @@ def __init__(self, file, fileNodeChunkReference): self.guidFooter = uuid.UUID(bytes_le=self.guidFooter) def __str__(self): - return self.FileData[:128].hex() + fd = self.FileData + if isinstance(fd, (bytes, bytearray)): + return fd[:128].hex() + if isinstance(fd, str): + return fd[:128] + return repr(fd)[:128] class ObjectSpaceObjectPropSet: @@ -668,7 +673,14 @@ def get_properties(self): try: propertyVal = self.rgData[i].Data.decode('utf-16') except: - propertyVal = self.rgData[i].Data.hex() + data = self.rgData[i].Data + if isinstance(data, (bytes, bytearray)): + propertyVal = data.hex() + elif isinstance(data, str): + # Already decoded somewhere upstream; pass through. + propertyVal = data + else: + propertyVal = repr(data) else: property_name_lower = propertyName.lower() if 'time' in property_name_lower: @@ -759,7 +771,11 @@ def __init__(self, file, propertySet): self.Data, = struct.unpack('{}s'.format(self.cb), file.read(self.cb)) def __str__(self): - return self.Data.hex() + if isinstance(self.Data, (bytes, bytearray)): + return self.Data.hex() + if isinstance(self.Data, str): + return self.Data + return repr(self.Data) class PropertyID: diff --git a/pyOneNote/OneDocument.py b/pyOneNote/OneDocument.py index 906a606..cad58ec 100644 --- a/pyOneNote/OneDocument.py +++ b/pyOneNote/OneDocument.py @@ -69,8 +69,19 @@ def get_global_identification_table(self): def get_json(self): files_in_hex = {} for key, file in self.get_files().items(): + content = file['content'] + if isinstance(content, (bytes, bytearray)): + content_hex = content.hex() + elif isinstance(content, str): + # Already a string; encode to bytes first to keep JSON output hex-only. + try: + content_hex = content.encode('latin-1').hex() + except UnicodeEncodeError: + content_hex = content.encode('utf-8', errors='replace').hex() + else: + content_hex = b''.hex() files_in_hex[key] = {'extension': file['extension'], - 'content': file['content'].hex(), + 'content': content_hex, 'identity': file['identity']} res = {