niftycode / imessage_reader

Fetch imessages from chat.db
MIT License
90 stars 22 forks source link

chokes on certain characters when exporting to Excel #19

Open ajkessel opened 1 year ago

ajkessel commented 1 year ago

I'm getting this error exporting to Excel:

Traceback (most recent call last):
  File "/Users/adam/Library/Python/3.11/bin/imessage_reader", line 8, in <module>
    sys.exit(main())
             ^^^^^^
  File "/Users/adam/Library/Python/3.11/lib/python/site-packages/imessage_reader/cli.py", line 116, in main
    check_database_path(args)
  File "/Users/adam/Library/Python/3.11/lib/python/site-packages/imessage_reader/cli.py", line 77, in check_database_path
    evaluate(MACOS_DB_PATH, args.output, args.recipients, args.version)
  File "/Users/adam/Library/Python/3.11/lib/python/site-packages/imessage_reader/cli.py", line 104, in evaluate
    data.show_user_txt("excel")
  File "/Users/adam/Library/Python/3.11/lib/python/site-packages/imessage_reader/fetch_data.py", line 125, in show_user_txt
    self._export_excel(fetched_data)
  File "/Users/adam/Library/Python/3.11/lib/python/site-packages/imessage_reader/fetch_data.py", line 143, in _export_excel
    ew.write_data()
  File "/Users/adam/Library/Python/3.11/lib/python/site-packages/imessage_reader/write_excel.py", line 89, in write_data
    sheet.cell(row=messages_row, column=2).value = message
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/adam/Library/Python/3.11/lib/python/site-packages/openpyxl/cell/cell.py", line 218, in value
    self._bind_value(value)
  File "/Users/adam/Library/Python/3.11/lib/python/site-packages/openpyxl/cell/cell.py", line 197, in _bind_value
    value = self.check_string(value)
            ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/adam/Library/Python/3.11/lib/python/site-packages/openpyxl/cell/cell.py", line 165, in check_string
    raise IllegalCharacterError(f"{value} cannot be used in worksheets.")

When I look at the message that it's choking on (with a text export), it appears to begin with ^YYour. I'm not sure what the ^Y is there for as the message itself just appears to be "Your..." in iMessages, but it seems like the extended character there is raising an exception. Could you sanitize the string to remove illegal characters?

niftycode commented 11 months ago

@ajkessel Thank you for your hint. I'll take a look at this problem.