string2ooxml

0.0.4 • Public • Published

string2ooxml

NodeJS环境下将字符串转为 Office Open XML,支持插入字符串文字,markdown 字符串片段


支持的功能

  • addSpecialText 将字符串转化为 ooxml,支持文字展示的基本样式设置
  • addMarkDown 将 markdown 片段转化为对应的 ooxml

安装

yarn add string2ooxml

or

npm install --save string2ooxml

快速上手

addSpecialText, addSpecialText 返回的是一个Promise函数

const { addSpecialText, addMarkDown } = require("string2ooxml");

// 转换文字
const textOoxml = addSpecialText({ text: "demo" });
textOoxml
  .then((resp) => {
    console.log("resp:%o", resp);
  })
  .catch((err) => {
    console.log("err:%o", err);
  });

/**
resp:'<w:p w:rsidR="00A77427" w:rsidRDefault="007F1D13"><w:pPr><w:ind/></w:pPr><w:r><w:t>demo 是的</w:t></w:r></w:p>'
*/

// 转换markdown片段
const mdStr = `
# 你好
## 展示一个demo
`;
const mdOoxml = addMarkDown({ mdStr });
mdOoxml
  .then((resp) => {
    console.log("resp:%o", resp);
  })
  .catch((err) => {
    console.log("err:%o", err);
  });

/**
resp:'<w:p w:rsidR="00A77427" w:rsidRDefault="007F1D13"><w:pPr><w:ind/></w:pPr><w:r><w:rPr><w:b/><w:bCs/><w:rFonts w:ascii="Arial" w:eastAsia="Arial" w:hAnsi="Arial" w:cs="Arial" /><w:sz w:val="36"/><w:szCs w:val="36"/></w:rPr><w:t>你好</w:t></w:r></w:p><w:p w:rsidR="00A77427" w:rsidRDefault="007F1D13"><w:pPr><w:ind/></w:pPr><w:r><w:rPr><w:b/><w:bCs/><w:rFonts w:ascii="Arial" w:eastAsia="Arial" w:hAnsi="Arial" w:cs="Arial" /><w:sz w:val="32"/><w:szCs w:val="32"/></w:rPr><w:t>展示一个demo</w:t></w:r></w:p>'
*/

string2ooxml API

注意:返回的是一个Promise函数options是一个Object对象

const { addSpecialText, addMarkDown } = require("string2ooxml");

addSpecialText(options);
addMarkDown(options);

参数配置

addSpecialText options 参数

参数 说明 类型 可选值 默认值
text 字符串 string any ''
style 字符串的样式 Object style option {}

style option

参数 说明 类型 可选值
color 字体颜色代码 string 例如:'ffffff'(白色)或 '000000'(黑色)
back 背景颜色代码 string 例如:'ffffff'(白色)或 '000000'(黑色)
bold true 使文本加粗 Boolean trueoffalse
border 边框 string 'single'、'dashDotStroked'、'dashed'、'dashSmallGap'、'dotDash'、'dotDotDash'、'dotted'、'double'、'thick'等
italic true 使文本斜体 Boolean trueoffalse
underline true 添加下划线 Boolean trueoffalse
font_face 要使用的字体 string
font_size pt为单位的字体大小 number
highlight 突出显示 string 'black', 'blue', 'cyan', 'darkBlue', 'darkCyan', 'darkGray', 'darkGreen', 'darkMagenta', 'darkRed', 'darkYellow', 'green', 'lightGray', 'magenta', 'none', 'red', 'white' or 'yellow'.
strikethrough true 添加删除线 Boolean trueoffalse
superscript 上标true 文本降低到基线以下并将其更改为更小的尺寸 Boolean trueoffalse
subscript 下标true 文本提高到基线以上并将其更改为较小的尺寸 Boolean trueoffalse

style option 其他更多参数, 详见officegen Create Microsoft Office Word Document Reference 文档

addMarkDown options 参数

注意:严重依赖换行空格

参数 说明 类型 可选值 默认值
mdStr markdown 片段 string any

markdown 目前支持的语法有

# 标题一*em*
## 标题二
### 标题三
#### 标题四
##### 标题五
###### 标题六
####### 文字
_em_
adssad**strong**fdsfdsg
**strong**

--- // 分割线 word 不兼容

// 列表类 wps 兼容有问题
- sadsa
- dfsaaf
- dsaf

1. 大萨达
2. 重复的开始搞
3. 鼓风机宽度是否

// 引用类 wps 兼容有问题
> react

// 链接类 wps 兼容有问题、
[百度](https://www.baidu.com)

案例

docxtemplater的支持插入xml功能结合,插入特殊字体或者markdown片段

设置数据不要使用 doc.setData(data)啦哈,直接使用doc.render(data)

docxtemplaterstring2ooxml结合要做一点小小的改动 修改 doc.render(data)为doc.renderAsync,其他不需要做任何变动

// 因为string2ooxml内部是异步处理,所以要将docxtemplater的render改为异步
doc.renderAsync({
  ...data,
  customText: addSpecialText({ text: "demo" }),
  customMd: addMarkDown({ mdstr: "# demo" }),
});

模板 docx 中使用

{@customText}
{@customMd}

开发和调试

  • npm install
  • npm link
  • 然后到你测试的项目中执行 npm link string2ooxml
  • 引入并使用

Package Sidebar

Install

npm i string2ooxml

Weekly Downloads

3

Version

0.0.4

License

MIT

Unpacked Size

27.8 kB

Total Files

11

Last publish

Collaborators

  • dingzhen