NodeJS使用winax调用WPS进行文档转换、文件服务器及WS服务器

前言

注意

安装的时候需要电脑上的Python版本为2.7,否则编译报错。

设置环境

1
2
npm config set python python2.7
npm config set msvs_version 2017

安装

1
npm install winax --msvs_version=2017

使用

1
2
var winax = require('winax');
var con = new winax.Object('ADODB.Connection');

利用WPS进行文档转换

文档格式转换

文档格式转换 支持 wps、wpt、doc、docx、dot、txt等所有文档格式文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
var winax = require('winax');
var Variant = winax.Variant;

var source_path = "D:\\Tools\\1.doc";
var target_path = "D:\\Tools\\1.pdf";

var wax = new winax.Object("KWPS.Application", {
activate: true
});

try {
var doc = wax.Documents.Open(new Variant(source_path), new Variant(true), new Variant(true));
doc.SaveAs(new Variant(target_path), new Variant(17));
doc.Close();
} catch (e) {
console.info(e);
} finally {
wax.Quit();
}

幻灯片格式转换

幻灯片格式转换 支持ppt、pps、pptx、ppsx、dps、dpt、pot、uof

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
var winax = require('winax');
var Variant = winax.Variant;

var source_path = "D:\\Tools\\1.pptx";
var target_path = "D:\\Tools\\2.pdf";

var wax = new winax.Object("KWPP.Application", {
activate: true
});

try {
var doc = wax.Presentations.Open(new Variant(source_path), new Variant(true));
doc.SaveAs(new Variant(target_path), new Variant(32));
doc.Close();
} catch (e) {
console.info(e);
} finally {
wax.Quit();
}

转换表格

表格格式转换 支持et、ett、xls、xlsx、xlt、uof、prn、csv

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
var winax = require('winax');
var Variant = winax.Variant;

var source_path = "D:\\Tools\\1.xlsx";
var target_path = "D:\\Tools\\3.pdf";

var wax = new winax.Object("KET.Application", {
activate: true
});

try {
var doc = wax.Workbooks.Open(new Variant(source_path), new Variant(0), new Variant(true));
doc.ExportAsFixedFormat(new Variant(0), new Variant(target_path), new Variant(0));
doc.Close();
} catch (e) {
console.info(e);
} finally {
wax.Quit();
}

文件服务器

安装依赖

1
2
npm install connect
npm install serve-static

代码

1
2
3
4
5
6
7
var connect = require("connect");
var serveStatic = require("serve-static");

var app = connect();
app.use(serveStatic("D:\\Tools"));
app.listen(5000);
console.info("文件服务器启动了!");

WS服务器

1
2
3
4
5
6
7
8
9
10
11
const WebSocketServer = require('ws').Server;
wss = new WebSocketServer({port: 12122});
wss.on('connection', (ws) => {
// 有客户端连接时, 打印一条日志
console.log('client connected');
// 并且创建'message'监听
ws.on('message', (message) => {
// 直接将消息打印出来
console.log(message);
});
});