vobject
vobject is a pure-Python package for parsing and creating iCalendar and vCard files. It supports Python 3.8 or later for the 1.x series (which is under active development, though 0.9.x is currently the latest stable release), while the 0.9.x series (including 0.9.9) supports Python 2.7 and earlier Python 3 versions. The latest version, 0.9.9, was released in December 2024. The project is actively maintained by the py-vobject organization on GitHub, with recent bug fixes and feature additions.
Warnings
- breaking The `vobject` library has two main series: 0.9.x and 1.x. Version 0.9.x (including the current 0.9.9) maintains compatibility with Python 2.7 and earlier Python 3 versions. The upcoming 1.x series will *only* support Python 3.8 and later. Be aware of this when planning Python environment upgrades or starting new projects.
- gotcha When parsing iCalendar or vCard strings, `vobject.readOne()` and `vobject.readComponents()` expect well-formed input, including closing `END:` tags for all components. Missing `END:` tags or malformed input will result in parsing errors (e.g., `SyntaxError` or `StopIteration`).
- gotcha Accessing properties directly via `item.contents['property_name']` will return a list, even if only one value exists for that property. You often need to access `item.contents['property_name'][0].value` to get the actual value. Convenience attributes like `item.summary.value` or `item.dtstart.value` are available for common, single-value properties.
- gotcha During serialization (`.serialize()`), `vobject` automatically adds mandatory iCalendar/vCard properties like `UID`, `DTSTAMP`, and `PRODID` if they are not already present. While usually beneficial for creating valid files, this can be unexpected if strict control over all output properties is desired before serialization.
- deprecated Users running `vobject` on Python 3.9+ may encounter `DeprecationWarning: invalid escape sequence` messages, primarily originating from internal regular expressions in `vobject/base.py`. These are warnings and do not stop execution but indicate potential future compatibility issues or a need for updated regex patterns.
Install
-
pip install vobject
Imports
- vobject
import vobject
- readOne
vobject.readOne(...)
- readComponents
vobject.readComponents(...)
- newFromBehavior
vobject.newFromBehavior('vcalendar') - iCalendar
vobject.iCalendar()
- vCard
vobject.vCard()
Quickstart
import vobject
import datetime
# Create a new iCalendar object
cal = vobject.iCalendar()
# Add an event component
event = cal.add('vevent')
# Add properties to the event
event.add('summary').value = 'Meeting with AI team'
event.add('dtstart').value = datetime.datetime(2026, 4, 15, 10, 0, 0, tzinfo=datetime.timezone.utc)
event.add('dtend').value = datetime.datetime(2026, 4, 15, 11, 0, 0, tzinfo=datetime.timezone.utc)
# Print the iCalendar object in a human-readable format
print('--- Pretty Print ---')
print(cal.prettyPrint())
# Serialize the iCalendar object to its string representation
# vobject will automatically add missing mandatory properties like UID and DTSTAMP
ical_string = cal.serialize()
print('\n--- Serialized iCalendar ---')
print(ical_string)
# Example of parsing a vCard from a string
vcard_string = """
BEGIN:VCARD
VERSION:3.0
N:Doe;John;;;
FN:John Doe
EMAIL;TYPE=WORK:john.doe@example.com
END:VCARD
"""
try:
parsed_vcard = vobject.readOne(vcard_string)
print('\n--- Parsed vCard (FN) ---')
print(parsed_vcard.fn.value)
except Exception as e:
print(f"Error parsing vCard: {e}")