class AttachmentItem
A data class representing metadata attributes of an attachment item, including its type, name, and size.
/tf/active/vicechatdev/SPFCsync/venv/lib64/python3.11/site-packages/office365/outlook/mail/attachments/attachment_item.py
6 - 28
simple
Purpose
AttachmentItem is a value object that encapsulates the essential attributes of a file or item to be attached in the Office 365 Outlook mail system. It inherits from ClientValue and provides a structured way to represent attachment metadata before the actual attachment is created. The class includes a factory method to create instances from file paths, automatically extracting file information.
Source Code
class AttachmentItem(ClientValue):
"""Represents attributes of an item to be attached."""
def __init__(self, attachment_type=None, name=None, size=None):
"""
:param int attachment_type:
:param str name:
:param int size:
"""
super(AttachmentItem, self).__init__()
self.attachmentType = attachment_type
self.name = name
self.size = size
@staticmethod
def create_file(path):
file_name = os.path.basename(path)
file_size = os.stat(path).st_size
from office365.outlook.mail.attachments.attachment_type import AttachmentType
return AttachmentItem(
attachment_type=AttachmentType.file, name=file_name, size=file_size
)
Parameters
| Name | Type | Default | Kind |
|---|---|---|---|
bases |
ClientValue | - |
Parameter Details
attachment_type: An integer representing the type of attachment (e.g., file, item, reference). Expected to be a value from the AttachmentType enumeration. Can be None if not specified during initialization.
name: A string representing the name of the attachment (typically the filename). Can be None if not specified during initialization.
size: An integer representing the size of the attachment in bytes. Can be None if not specified during initialization.
Return Value
Instantiation returns an AttachmentItem object with the specified attachment_type, name, and size attributes. The create_file static method returns a fully populated AttachmentItem instance with metadata extracted from the provided file path.
Class Interface
Methods
__init__(self, attachment_type=None, name=None, size=None)
Purpose: Initializes an AttachmentItem instance with optional attachment metadata
Parameters:
attachment_type: Integer representing the attachment type (default: None)name: String representing the attachment name (default: None)size: Integer representing the attachment size in bytes (default: None)
Returns: None (constructor)
create_file(path) -> AttachmentItem
static
Purpose: Factory method that creates an AttachmentItem instance from a file path, automatically extracting file metadata
Parameters:
path: String path to the file from which to create the attachment item
Returns: AttachmentItem instance populated with file metadata (name, size, and type set to AttachmentType.file)
Attributes
| Name | Type | Description | Scope |
|---|---|---|---|
attachmentType |
int or None | Stores the type of attachment as an integer value, typically from AttachmentType enumeration | instance |
name |
str or None | Stores the name of the attachment, typically the filename | instance |
size |
int or None | Stores the size of the attachment in bytes | instance |
Dependencies
osoffice365.runtime.client_valueoffice365.outlook.mail.attachments.attachment_type
Required Imports
import os
from office365.runtime.client_value import ClientValue
Conditional/Optional Imports
These imports are only needed under specific conditions:
from office365.outlook.mail.attachments.attachment_type import AttachmentType
Condition: only when using the create_file static method
Required (conditional)Usage Example
# Manual instantiation
attachment = AttachmentItem(attachment_type=1, name='document.pdf', size=1024000)
# Using the factory method to create from file
attachment = AttachmentItem.create_file('/path/to/document.pdf')
# Accessing attributes
print(attachment.name) # 'document.pdf'
print(attachment.size) # file size in bytes
print(attachment.attachmentType) # AttachmentType.file
Best Practices
- Use the create_file static method when creating AttachmentItem from existing files to automatically populate metadata
- Ensure the file path exists and is accessible before calling create_file to avoid FileNotFoundError
- This is a value object (inherits from ClientValue), so it should be treated as immutable after creation
- The attachment_type should correspond to values defined in the AttachmentType enumeration for consistency
- This class only stores metadata; it does not handle the actual file content or upload process
- Instance attributes can be None if not provided during initialization, so handle potential None values when accessing
Similar Components
AI-powered semantic similarity - components with related functionality:
-
class AttachmentInfo 80.4% similar
-
class ItemAttachment 78.2% similar
-
class Attachment 76.6% similar
-
class FileAttachment 74.7% similar
-
class AttachmentCreationInformation 71.5% similar