tnefparse
raw JSON → 1.4.0 verified Mon Apr 27 auth: no python
tnefparse is a pure Python library for decoding TNEF (Transport Neutral Encapsulation Format) files, commonly used by Microsoft Outlook to encapsulate rich email content. Version 1.4.0 is the latest release, with no external dependencies. It has a low release cadence.
pip install tnefparse Common errors
error TypeError: a bytes-like object is required, not 'str' ↓
cause Passing a string instead of bytes to TNEF constructor.
fix
Open the file in binary mode: with open('winmail.dat', 'rb') as f: data = f.read()
error AttributeError: 'TNEF' object has no attribute 'files' ↓
cause Using deprecated 'files' attribute in newer versions (>=1.3).
fix
Use 'tnef.attachments' instead of 'tnef.files'.
Warnings
gotcha Attachment names may include leading slash from Unix path; strip it before saving to avoid writing to root. ↓
fix Use os.path.basename() or att.name.lstrip('/') when writing files.
deprecated The old method 'tnef.files' was deprecated in favor of 'tnef.attachments'. ↓
fix Use tnef.attachments instead of tnef.files.
gotcha TNEF data passed to constructor must be bytes, not string. Decoding first will cause TypeError. ↓
fix Open file in binary mode ('rb') or call .encode() on string.
Imports
- TNEF wrong
import tnefparse.TNEFcorrectfrom tnefparse import TNEF - TNEFAttachment
from tnefparse import TNEFAttachment
Quickstart
import os
from tnefparse import TNEF
with open('winmail.dat', 'rb') as f:
data = f.read()
tnef = TNEF(data)
print('Attachments:', len(tnef.attachments))
for att in tnef.attachments:
print('Name:', att.name)
if att.name:
with open(att.name.lstrip('/'), 'wb') as out:
out.write(att.data)