yuzhi-utils
v1.1.20
Published
禹治科技开发工具库,包含 OE1 参数格式化,JSON格式匹配,翻译工具及pdf转txt工具等。
Downloads
67
Readme
禹治科技工具库
禹治科技开发工具库,包含 OE1 参数格式化,JSON格式匹配等。
1. OE1 参数格式化
返回值格式参考
[
{
"value": "1W (USB powered, 5VDC, 200 mA)",
"numVal": null,
"unit": null,
"type": 3,
"maxValue": null,
"minValue": null,
"optionVal": null,
"name": "电源",
"cnName": "Power"
},
...
]
示例
import { specFormat } from 'yuzhi-utils';
const specObj = {
"分辨率[#$]Resolution": "256 px",
"探测波段[#$]Band of detection": "MWIR (1 - 5 microns)",
...
}
// specObj 参数格式对象 [#$] 为中文与英文中间的分隔符
specFormat(specObj, '[#$]')
2. JSON 格式匹配
示例
import { jsonMatch } from 'yuzhi-utils';
// str 为字符串
const jsonObj = jsonMatch(str)
console.log(jsonObj); // 如果匹配成功,返回对象,匹配失败,返回空对象
3. 翻译API
示例
import { YoudaoTranslate, DeepLTranslate } from '../index'
// 有道翻译
const translater = new YoudaoTranslate({
appKey: 'appKey',
appSecret: 'appSecret'
})
async function run() {
try {
const texts = `I am a personal\nhello world`;
const result = await translater.translate(texts);
console.log('result=>', result)
} catch (error) {
console.log('Error:', error)
}
}
run();
// DeepL翻译 默认英->中
const deepLTranslate = new DeepLTranslate({
appKey: 'fd7ef643-2c38-8a55-0ad2-9a14961b420d'
})
async function run() {
try {
// 将文本翻译为中文
const translateStr = `我需要一个苹果\nhello world`
const deepRes = await deepLTranslate.translate(translateStr, 'en', 'zh');
console.log('result=>', deepRes)
} catch (error) {
console.log('Error:', error)
}
}
run();
4. PDF -> txt 工具
示例
import { Pdf2Txt } from '../index'
import path from 'path'
async function run() {
try {
const converter = new Pdf2Txt();
// convert的output路径可选,生成的文本文件默认会放在与pdf来源路径下的text目录下
const res = await converter.convert(path.join(__dirname, './pdf/6983713723465093121.pdf'))
// 如果转换成功,返回对象中,data为Buffer
if(res.msg === 'success') {
console.log('文本内容:', Buffer.from(res.data || '').toString('utf-8'))
console.log('转换成功')
}
} catch (error) {
console.log(error)
}
}
run();
5. PDF -> img 从PDF中提取图片
需要使用pip安装 pymupdf
pillow
pip install pymupdf pillow
or
pip3 install pymupdf pillow
示例
import { Pdf2Img } from '../index'
import path from 'path'
async function start() {
try {
const converter = new Pdf2Img();
// convert的output路径可选,提取的图片默认放在同目录的img文件夹下
const res = await converter.convert(path.join(__dirname, './pdf/6983713723465093121.pdf'))
if(res.msg === 'success') {
console.log('图片路径信息:',res.data)
}
} catch (error) {
console.log(error)
}
}
start();