officegen

2023-05-22

前端

以权利合者,权利尽而交疏。——《史记》

如果我们需要使用js来编辑wordexcelpowerpoint

可以使用这个开源项目:https://github.com/Ziv-Barber/officegen

image-20230522212408914

可以让我们在html中进行编辑

安装使用:

1
$ npm install officegen

ppt例子:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
const officegen = require('officegen')
const fs = require('fs')

// Create an empty PowerPoint object:
let pptx = officegen('pptx')

// Let's add a title slide:

let slide = pptx.makeTitleSlide('Officegen', 'Example to a PowerPoint document')

// Pie chart slide example:

slide = pptx.makeNewSlide()
slide.name = 'Pie Chart slide'
slide.back = 'ffff00'
slide.addChart(
{
title: 'My production',
renderType: 'pie',
data:
[
{
name: 'Oil',
labels: ['Czech Republic', 'Ireland', 'Germany', 'Australia', 'Austria', 'UK', 'Belgium'],
values: [301, 201, 165, 139, 128, 99, 60],
colors: ['ff0000', '00ff00', '0000ff', 'ffff00', 'ff00ff', '00ffff', '000000']
}
]
}
)

// Let's generate the PowerPoint document into a file:

return new Promise((resolve, reject) => {
let out = fs.createWriteStream('example.pptx')

// This one catch only the officegen errors:
pptx.on('error', function(err) {
reject(err)
})

// Catch fs errors:
out.on('error', function(err) {
reject(err)
})

// End event after creating the PowerPoint file:
out.on('close', function() {
resolve()
})

// This async method is working like a pipe - it'll generate the pptx data and put it into the output stream:
pptx.generate(out)
})

非常地好用