### Install pptxtojson with npm
Source: https://github.com/pipipi-pikachu/pptxtojson/blob/master/README.md
Install the library using npm. This is the first step before using it in your project.
```bash
npm install pptxtojson
```
--------------------------------
### Node.js Usage Example (Experimental)
Source: https://github.com/pipipi-pikachu/pptxtojson/blob/master/README.md
Example for using pptxtojson in a Node.js environment. Requires version 1.5.0 or higher. Reads a local .pptx file and parses it.
```javascript
const pptxtojson = require('pptxtojson/dist/index.cjs')
const fs = require('fs')
async function func() {
const buffer = fs.readFileSync('test.pptx')
const json = await pptxtojson.parse(buffer.buffer, {
imageMode: 'base64',
videoMode: 'none',
audioMode: 'none',
})
console.log(json)
}
func()
```
--------------------------------
### Browser Usage Example
Source: https://github.com/pipipi-pikachu/pptxtojson/blob/master/README.md
Example of how to use pptxtojson in a browser environment. It involves an input file element and FileReader API to process the .pptx file.
```html
```
```javascript
import { parse } from 'pptxtojson'
document.querySelector('input').addEventListener('change', evt => {
const file = evt.target.files[0]
const reader = new FileReader()
reader.onload = async e => {
const json = await parse(e.target.result, {
imageMode: 'base64',
videoMode: 'none',
audioMode: 'none',
})
console.log(json)
}
reader.readAsArrayBuffer(file)
})
```
--------------------------------
### pptxtojson Output Structure
Source: https://github.com/pipipi-pikachu/pptxtojson/blob/master/README.md
An example of the JSON output structure generated by pptxtojson. It includes slides, theme colors, and document size.
```json
{
"slides": [
{
"fill": {
"type": "color",
"value": "#FF0000"
},
"elements": [
{
"left": 0,
"top": 0,
"width": 72,
"height": 72,
"borderColor": "#1F4E79",
"borderWidth": 1,
"borderType": "solid",
"borderStrokeDasharray": 0,
"fill": {
"type": "color",
"value": "#FF0000"
},
"content": "
TEST
",
"isFlipV": false,
"isFlipH": false,
"rotate": 0,
"vAlign": "mid",
"name": "矩形 1",
"type": "shape",
"shapType": "rect"
},
// more...
],
"layoutElements": [
// more...
],
"note": "演讲者备注内容..."
},
// more...
],
"themeColors": ['#4472C4', '#ED7D31', '#A5A5A5', '#FFC000', '#5B9BD5', '#70AD47'],
"size": {
"width": 960,
"height": 540
}
}
```
--------------------------------
### Parse .pptx File with pptxtojson
Source: https://context7.com/pipipi-pikachu/pptxtojson/llms.txt
Use the `parse` function to convert a .pptx file (as an ArrayBuffer) into a JSON object. Configure image, video, and audio output modes. This example shows browser-side file input and Node.js usage.
```javascript
import { parse } from 'pptxtojson'
// 浏览器端:监听 上传事件
document.querySelector('input[type="file"]').addEventListener('change', async (evt) => {
const file = evt.target.files[0]
const reader = new FileReader()
reader.onload = async (e) => {
try {
const json = await parse(e.target.result, {
imageMode: 'base64', // 图片以 base64 输出(可选 'blob' | 'both' | 'none')
videoMode: 'blob', // 视频以 blob URL 输出(可选 'none')
audioMode: 'blob', // 音频以 blob URL 输出(可选 'none')
})
console.log('幻灯片总数:', json.slides.length)
console.log('幻灯片尺寸:', json.size) // { width: 960, height: 540 }
console.log('主题色:', json.themeColors) // ['#4472C4', '#ED7D31', ...]
console.log('内嵌字体:', json.usedFonts) // ['微软雅黑', 'Calibri', ...]
console.log('第一页元素:', json.slides[0].elements)
} catch (err) {
console.error('解析失败:', err)
}
}
reader.readAsArrayBuffer(file)
})
// Node.js 端(1.5.0+ 实验性支持)
const pptxtojson = require('pptxtojson/dist/index.cjs')
const fs = require('fs')
async function parseFile() {
const buffer = fs.readFileSync('presentation.pptx')
const json = await pptxtojson.parse(buffer.buffer, {
imageMode: 'base64',
videoMode: 'none',
audioMode: 'none',
})
console.log(JSON.stringify(json.slides[0], null, 2))
}
parseFile()
```
--------------------------------
### Options Configuration
Source: https://github.com/pipipi-pikachu/pptxtojson/blob/master/README.md
Explanation of the available options for the `parse` function, which control how media resources are handled.
```markdown
The `parse` function accepts an optional `options` object with the following properties:
- `imageMode`: Controls image resource parsing. Options: `base64`, `blob`, `both`, `none`. Default: `base64`.
- `base64`: Parses only base64 encoded images.
- `blob`: Parses only blob images.
- `both`: Parses both base64 and blob images.
- `none`: Does not parse image content.
- `videoMode`: Controls video resource parsing. Options: `blob`, `none`. Default: `none`.
- `blob`: Parses video as blob.
- `none`: Does not parse video content.
- `audioMode`: Controls audio resource parsing. Options: `blob`, `none`. Default: `none`.
- `blob`: Parses audio as blob.
- `none`: Does not parse audio content.
```
--------------------------------
### parse(file, options?)
Source: https://context7.com/pipipi-pikachu/pptxtojson/llms.txt
The main entry point for parsing a .pptx file. It accepts an ArrayBuffer and optional configuration, returning a JSON object with slides, theme colors, fonts, and dimensions.
```APIDOC
## parse(file, options?)
### Description
This is the sole public export function of the library. It takes an `ArrayBuffer` of a .pptx file and optional configuration settings. It asynchronously returns a JSON object containing all slide data, including slides, theme colors, used fonts, and slide dimensions.
### Parameters
#### Path Parameters
None
#### Query Parameters
None
#### Request Body
- **file** (ArrayBuffer) - Required - The .pptx file content as an ArrayBuffer.
- **options** (object) - Optional - Configuration options for parsing.
- **imageMode** (string) - Optional - Controls how image resources are output. Supported values: `'base64'` (default), `'blob'`, `'both'`, `'none'`.
- **videoMode** (string) - Optional - Controls how video resources are output. Supported values: `'blob'` (default), `'none'`.
- **audioMode** (string) - Optional - Controls how audio resources are output. Supported values: `'blob'` (default), `'none'`.
### Request Example
```javascript
import { parse } from 'pptxtojson'
// Browser example
document.querySelector('input[type="file"]').addEventListener('change', async (evt) => {
const file = evt.target.files[0]
const reader = new FileReader()
reader.onload = async (e) => {
try {
const json = await parse(e.target.result, {
imageMode: 'base64',
videoMode: 'blob',
audioMode: 'blob',
})
console.log('Total slides:', json.slides.length)
console.log('Slide size:', json.size)
console.log('Theme colors:', json.themeColors)
console.log('Used fonts:', json.usedFonts)
console.log('First slide elements:', json.slides[0].elements)
} catch (err) {
console.error('Parsing failed:', err)
}
}
reader.readAsArrayBuffer(file)
})
// Node.js example (experimental support for 1.5.0+)
const pptxtojson = require('pptxtojson/dist/index.cjs')
const fs = require('fs')
async function parseFile() {
const buffer = fs.readFileSync('presentation.pptx')
const json = await pptxtojson.parse(buffer.buffer, {
imageMode: 'base64',
videoMode: 'none',
audioMode: 'none',
})
console.log(JSON.stringify(json.slides[0], null, 2))
}
parseFile()
```
### Response
#### Success Response (200)
- **slides** (array) - An array of slide objects, each containing elements, shapes, etc.
- **themeColors** (array) - An array of theme color strings (e.g., hex codes).
- **usedFonts** (array) - An array of strings representing the fonts used in the presentation.
- **size** (object) - An object with `width` and `height` properties indicating the slide dimensions in points.
#### Response Example
```json
{
"slides": [
{
"elements": [
{
"type": "text",
"content": "Example Text",
"x": 100,
"y": 100,
"width": 200,
"height": 50
}
]
}
],
"themeColors": ["#4472C4", "#ED7D31"],
"usedFonts": ["微软雅黑", "Calibri"],
"size": {"width": 960, "height": 540}
}
```
```
--------------------------------
### options.imageMode
Source: https://context7.com/pipipi-pikachu/pptxtojson/llms.txt
Configures the output format for image resources within slides, supporting base64, blob, both, or none.
```APIDOC
## options.imageMode
### Description
This option controls the output format for image resources, including background images, image elements, and images used to fill shapes. It supports four modes: `'base64'` (default), `'blob'`, `'both'`, and `'none'`. Image data is stored in the `base64` and/or `blob` fields of the element, while the `ref` field always retains the internal file path reference.
### Parameters
#### Path Parameters
None
#### Query Parameters
None
#### Request Body
- **imageMode** (string) - Required - Specifies the image output format. Options:
- `'base64'`: Outputs images as base64 encoded strings. Suitable for server-side rendering or scenarios not relying on `URL.createObjectURL`.
- `'blob'`: Outputs images as Blob URLs. Optimized for direct display in browsers and better memory management.
- `'both'`: Outputs both base64 and blob URLs.
- `'none'`: Does not parse image content, only retains the path reference. This is the fastest option.
### Request Example
```javascript
// Only output base64 (suitable for server-side rendering or scenarios not relying on URL.createObjectURL)
const jsonBase64 = await parse(arrayBuffer, { imageMode: 'base64' })
const imgEl = jsonBase64.slides[0].elements.find(el => el.type === 'image')
// imgEl.base64 => "data:image/png;base64,iVBORw0KGgo..."
// imgEl.blob => ""
// Only output blob (suitable for direct display in browsers, more memory efficient)
const jsonBlob = await parse(arrayBuffer, { imageMode: 'blob' })
const imgEl2 = jsonBlob.slides[0].elements.find(el => el.type === 'image')
// imgEl2.blob => "blob:http://localhost/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
// imgEl2.base64 => ""
// Output both base64 and blob
const jsonBoth = await parse(arrayBuffer, { imageMode: 'both' })
// Do not parse image content (only retain path reference, fastest)
const jsonNone = await parse(arrayBuffer, { imageMode: 'none' })
const imgEl3 = jsonNone.slides[0].elements.find(el => el.type === 'image')
// imgEl3.ref => "ppt/media/image1.png"
// imgEl3.base64 => ""
// imgEl3.blob => ""
```
### Response
#### Success Response (200)
- **slides** (array) - Array of slide objects. Each object may contain an `elements` array with image elements.
- **elements[].base64** (string) - Base64 encoded image data if `imageMode` is `'base64'` or `'both'`.
- **elements[].blob** (string) - Blob URL for the image if `imageMode` is `'blob'` or `'both'`.
- **elements[].ref** (string) - Internal path reference to the image file.
#### Response Example
```json
{
"slides": [
{
"elements": [
{
"type": "image",
"ref": "ppt/media/image1.png",
"base64": "data:image/png;base64,iVBORw0KGgo...",
"blob": "blob:http://localhost/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
}
]
}
]
}
```
```
--------------------------------
### Parse PPTX and Log Slide Elements
Source: https://context7.com/pipipi-pikachu/pptxtojson/llms.txt
Parses a PPTX file into JSON and logs information about slide layout elements and content elements. The rendering order is demonstrated, with layout elements appearing below content elements.
```javascript
const json = await parse(arrayBuffer, { imageMode: 'base64' })
const slide = json.slides[0]
// 渲染顺序:layoutElements(背景装饰)在下,elements(内容)在上
const allElements = [...slide.layoutElements, ...slide.elements]
allElements.sort((a, b) => (a.order || 0) - (b.order || 0))
allElements.forEach(el => {
console.log(`[${el.type}] order=${el.order}`, el.left, el.top)
})
```
--------------------------------
### Parse Video and Audio Elements - 'video' / 'audio'
Source: https://context7.com/pipipi-pikachu/pptxtojson/llms.txt
Extract video and audio elements, controlling resource loading with `videoMode` and `audioMode`. Supports various formats and provides direct URLs for external resources.
```javascript
const json = await parse(arrayBuffer, {
videoMode: 'blob',
audioMode: 'blob',
})
for (const el of json.slides[0].elements) {
if (el.type === 'video') {
console.log('视频引用路径:', el.ref) // 'ppt/media/video1.mp4' 或外链 URL
console.log('视频 blob URL:', el.blob) // 'blob:http://...' 或 ''
// 可直接绑定到