一个运行在浏览器中,可以将 .pptx 文件转为可读的 JSON 数据的 JavaScript 库。
与其他的pptx文件解析工具的最大区别在于:
- 直接运行在浏览器端;
- 解析结果是可读的 JSON 数据,而不仅仅是把 XML 文件内容原样翻译成难以理解的 JSON。
在线DEMO:https://pipipi-pikachu.github.io/pptxtojson/
本仓库诞生于项目 PPTist ,希望为其“导入 .pptx 文件功能”提供一个参考示例。不过就目前来说,解析出来的PPT信息与源文件在样式上还是存在差异。
但如果你只是需要提取PPT文件的文本内容、媒体资源信息、结构信息等,或者对排版/样式精准度没有特别高的要求,那么 pptxtojson 可能会对你有帮助。
输出的JSON中,所有数值长度值单位都为pt(point)
注意:在0.x版本中,所有输出的长度值单位都是px(像素)
npm install pptxtojson
<input type="file" accept="application/vnd.openxmlformats-officedocument.presentationml.presentation"/>
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)
console.log(json)
}
reader.readAsArrayBuffer(file)
})
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)
console.log(json)
}
func()
{
"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": "<p style=\"text-align: center;\"><span style=\"font-size: 18pt;font-family: Calibri;\">TEST</span></p>",
"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
}
}
幻灯片主题色 themeColors
幻灯片尺寸 size
widthheight幻灯片页面 slides
页面备注 note
页面背景填充(颜色、图片、渐变、图案) fill
type='color'type='image'type='gradient'type='pattern'页面切换动画 transition
typedurationdirection页面内元素 elements / 母版元素 layoutElements
文字
type='text'lefttopwidthheightborderColorborderWidthborderTypeborderStrokeDasharrayshadowfillcontentisFlipVisFlipHrotatevAlignisVerticalnameautoFit
type
shape:文本框高度会根据文本内容自动调整text:文本框大小固定,字号会自动缩放以适应文本框(注:autoFit不存在时,也会固定文本框大小,但字号不会缩放)fontScale图片
type='image'lefttopwidthheightborderColorborderWidthborderTypeborderStrokeDasharraygeomrectsrcrotatefilters形状
type='shape'lefttopwidthheightborderColorborderWidthborderTypeborderStrokeDasharrayshadowfillcontentisFlipVisFlipHrotateshapTypevAlignpathkeypointsnameautoFit表格
type='table'lefttopwidthheightbordersdatarowHeightscolWidths图表
type='chart'lefttopwidthheightdatacolorschartTypebarDirmarkerholeSizegroupingstyle视频
type='video'lefttopwidthheightblobsrc音频
type='audio'lefttopwidthheightblob公式
type='math'lefttopwidthheightpicBase64latextextSmart图
type='diagram'lefttopwidthheightelementstextList多元素组合
type='group'lefttopwidthheightelementshttps://github.com/pipipi-pikachu/pptxtojson/blob/master/dist/index.d.ts
本仓库大量参考了 PPTX2HTML 和 PPTXjs 的实现。
与它们不同的是:PPTX2HTML 和 PPTXjs 是将PPT文件转换为能够运行的 HTML 页面,而 pptxtojson 做的是将PPT文件转换为干净的 JSON 数据,且在原有基础上进行了大量优化补充(包括代码质量和提取信息的完整度和准确度)。
MIT License | Copyright © 2020-PRESENT pipipi-pikachu