> For AI agents: the complete documentation index is available at https://anyformat.swc.rs/llms.txt, the full documentation bundle is available at https://anyformat.swc.rs/llms-full.txt.

# Office

The office document family identifies office document formats from the leading
bytes of a file.

| Language | Package                 | Module                  |
| -------- | ----------------------- | ----------------------- |
| Node.js  | `@swc/anyformat-office` | `@swc/anyformat-office` |
| Python   | `anyformat-office`      | `anyformat_office`      |

## Supported formats

| Identifier | MIME type                                                                   | Format                                                                         |
| ---------- | --------------------------------------------------------------------------- | ------------------------------------------------------------------------------ |
| `docx`     | `application/vnd.openxmlformats-officedocument.wordprocessingml.document`   | Office Open XML word processing document                                       |
| `xlsx`     | `application/vnd.openxmlformats-officedocument.spreadsheetml.sheet`         | Office Open XML spreadsheet                                                    |
| `pptx`     | `application/vnd.openxmlformats-officedocument.presentationml.presentation` | Office Open XML presentation                                                   |
| `odt`      | `application/vnd.oasis.opendocument.text`                                   | OpenDocument text document                                                     |
| `ods`      | `application/vnd.oasis.opendocument.spreadsheet`                            | OpenDocument spreadsheet                                                       |
| `odp`      | `application/vnd.oasis.opendocument.presentation`                           | OpenDocument presentation                                                      |
| `cfb`      | `application/x-cfb`                                                         | Compound File Binary, the container of legacy `.doc`, `.xls`, and `.ppt` files |
| `rtf`      | `application/rtf`                                                           | Rich Text Format                                                               |

## `detect`

Identifies the office document format of `data` from its leading bytes. Returns
`null` in Node.js and `None` in Python when the format is not recognized.

For the ZIP-based formats, `data` must contain the local file headers up to the
first entry that identifies the format. Office applications write that entry
near the start of the file, so the first few kilobytes are usually enough.

### Node.js

```ts
function detect(data: Uint8Array): DetectedFormat | null
```

```js
import { readFile } from 'node:fs/promises'
import { detect } from '@swc/anyformat-office'

detect(await readFile('report.docx'))
// => { format: 'docx', mimeType: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document' }
```

### Python

```python
def detect(data: bytes) -> DetectedFormat | None
```

```python
from pathlib import Path

import anyformat_office

anyformat_office.detect(Path("report.docx").read_bytes())
# => DetectedFormat(format='docx', mime_type='application/vnd.openxmlformats-officedocument.wordprocessingml.document')
```

## `DetectedFormat`

A format identified by `detect`.

| Node.js    | Python      | Type     | Description                                                          |
| ---------- | ----------- | -------- | -------------------------------------------------------------------- |
| `format`   | `format`    | `string` | The stable lowercase identifier of the format, for example `"docx"`. |
| `mimeType` | `mime_type` | `string` | The most common MIME type of the format.                             |

In Python, `DetectedFormat` is a final class with read-only properties, and the
type of each property is `str`.

## `supportedFormats`

Returns the identifier of each format that `detect` can return.

### Node.js

```ts
function supportedFormats(): string[]
```

```js
import { supportedFormats } from '@swc/anyformat-office'

supportedFormats()
// => ['docx', 'xlsx', 'pptx', 'odt', 'ods', 'odp', 'cfb', 'rtf']
```

### Python

```python
def supported_formats() -> list[str]
```

```python
import anyformat_office

anyformat_office.supported_formats()
# => ['docx', 'xlsx', 'pptx', 'odt', 'ods', 'odp', 'cfb', 'rtf']
```
