wps格式批量转docx

admin2021-10-28  292

一个小工具,wps格式批量转docx

这里调用了金山WPS提供的COM对象,称为wps v9 api,类似于Office Word的VBA,详细的文档可以参考

WPS 基础接口>文字 API 参考>Document>Document 对象

同样可以完成一些自动化的操作,这方面文档非常少,建议参考资料
1. Word VBA学习教程
2. python win32调用com对象操作word
3. aardio标准库对excel COM编程的封装
import win.ui;
/*DSG{{*/
var winform = win.form(text="aardio form";right=303;bottom=199)
winform.add(
button={cls="button";text="选择目录";left=16;top=160;right=88;bottom=192;font=LOGFONT(name='宋体');z=2};
button2={cls="button";text="转换为docx";left=216;top=160;right=288;bottom=192;font=LOGFONT(name='宋体');z=3};
edit={cls="edit";left=16;top=8;right=288;bottom=144;edge=1;font=LOGFONT(name='宋体');multiline=1;z=1}
)
/*}}*/

import fsys;
import fsys.dlg.dir;

// 选择目录下的所有wps文件
winform.button.oncommand = function(id, event) {
	var path = fsys.dlg.dir(, winform, '请选择包含wps文件的目录') if (not path) {
		return;
	}
	global.WpsFiles = {};
	fsys.enum(path, "*.wps",
	function(dir, filename, fullpath, findData) {
		if (filename) {
			table.push(WpsFiles, fullpath);
		}
	},
	true);

	for (i = 1;#WpsFiles; 1) {
		winform.edit.printf('%s', WpsFiles[i])
	}

}

// 转换为docx
winform.button2.oncommand = function(id, event) {
	var WpsObject = com.TryCreateObject("KWPS.Application");
	if (not WpsObject) {
		winform.msgWarn("请先安装WPS专业版");
		return;
	}

	for (i = 1;#WpsFiles; 1) {
		var path = WpsFiles[i];
		//var ext = string.match(path,"[^.] $");
		//var ext = fsys.getExtensionName(path);
		var newPath = path   ".docx";

		if (io.exist(newPath)) {
			continue;
		}

		try {
			WpsObject.Visible = false;
			WpsObject.Documents.Open(path);
			WpsObject.ActiveDocument.SaveAs2(path   ".docx", 16);
			WpsObject.ActiveDocument.Close();
			//WpsFiles
		} catch(e) {
			//continue;
		}
	}

	if (WpsObject) {
		com.Release(WpsObject);
		WpsObject = nil;
	}

	winform.msgOk("转换完成");

}

winform.show();
win.loopMessage();
转载请注明原文地址: http://aardio.net/read-257.html
最新回复(0)
aardio问答
aardio编程语言