doct
doct
doct模块用于解析docx 、markdown 、epub 、pdf 、pptx、csv等文件
docx 对象
import {docx} from "doct";
方法
| 名称 | 类型 | 参数 | 返回值 | 说明 |
|---|---|---|---|---|
readFile | 方法 | path: string | DocxFile | 初始化点击验证码 |
readBuffer | 方法 | buffer: Buffer | DocxFile | 初始化滑动验证码 |
readFile
从本地文件系统读取docx文件。
参数:
path: docx文件的路径
返回:
DocxFile: 解析后的docx对象
示例:
const docFile = docx.readFile("./example.docx");
readBuffer
从内存缓冲区读取docx文件。
参数:
buffer: 包含docx文件内容的Buffer对象
返回:
Docx: 解析后的docx对象
示例:
const buffer = fs.readFileSync("./example.docx");
const docFile = docx.readBuffer(buffer);
DocxFile
DocxFile对象表示一个解析后的Word文档,包含文档的内容、样式和关系等信息。该对象由readFile或readBuffer方法返回。
方法
| 名称 | 类型 | 参数 | 返回值 | 说明 |
|---|---|---|---|---|
slice | 方法 | size: int | array: Object | 将文档切分为结构化数据 |
text | 方法 | 无 | String | 转纯文本 |
markdown | 方法 | 无 | String | 转md文档 |
slice
将文档切分为结构化数据
参数:
size: 每个切片的最大长度
返回:
array: Object: 对象,每个对象包含以下属性:id: 标题唯一标识符name: 标题文本content: 标题下的文本内容数组children: 子标题对象数组
示例:
const docFile = docx.readFile("./example.docx");
const objects = docFile.slice(600);
示例输出:
[{
"id": "1",
"name": "一级标题",
"content": [...],
"children": [{
"id": "1.1",
"name": "二级标题",
"content": [...],
"children": []
}]
}]
text
示例:
const docFile = docx.readFile("/path/to/file/通知.docx");
const text = docFile.text();
console.log(text);
markdown
markdown 对象
import {markdown} from "doct";
方法
| 名称 | 类型 | 参数 | 返回值 | 说明 |
|---|---|---|---|---|
readFile | 方法 | path: string | MarkdownFile | 从本地文件系统读取markdown文件 |
readBuffer | 方法 | buffer: Buffer | MarkdownFile | 从内存缓冲区读取markdown文件 |
readFile
从本地文件系统读取markdown文件。
参数:
path: markdown文件的路径
返回:
MarkdownFile: 解析后的markdown对象
示例:
const mdFile = markdown.readFile("./example.md");
readBuffer
从内存缓冲区读取markdown文件。
参数:
buffer: 包含markdown文件内容的Buffer对象
返回:
MarkdownFile: 解析后的markdown对象
示例:
const buffer = fs.readFileSync("./example.md");
const mdFile = markdown.readBuffer(buffer);
MarkdownFile
MarkdownFile对象表示一个解析后的Markdown文档,包含文档的内容和结构化信息。该对象由readFile或readBuffer方法返回。
方法
| 名称 | 类型 | 参数 | 返回值 | 说明 |
|---|---|---|---|---|
slice | 方法 | size: int | array: Object | 将文档切分为结构化数据 |
text | 方法 | 无 | String | 转纯文本 |
slice
将文档切分为结构化数据。
参数:
size: 每个切片的最大长度
返回:
array: Object: 对象数组,每个对象包含以下属性:id: 标题唯一标识符name: 标题文本content: 标题下的文本内容数组children: 子标题对象数组
示例:
const mdFile = markdown.readFile("./example.md");
const objects = mdFile.slice(600);
示例输出:
[{
"id": "1",
"name": "一级标题",
"content": [...],
"children": [{
"id": "1.1",
"name": "二级标题",
"content": [...],
"children": []
}]
}]
text
示例:
const mdFile = markdown.readFile("./example.md");
const text = mdFile.text();
console.log(text);
epub 对象
import {epub} from "doct";
方法
| 名称 | 类型 | 参数 | 返回值 | 说明 |
|---|---|---|---|---|
readFile | 方法 | path: string | EpubFile | 从本地文件系统读取epub文件 |
readBuffer | 方法 | buffer: Buffer | EpubFile | 从内存缓冲区读取epub文件 |
readFile
从本地文件系统读取epub文件。
参数:
path: epub文件的路径
返回:
EpubFile: 解析后的epub对象
示例:
const epubFile = epub.readFile("./example.epub");
readBuffer
从内存缓冲区读取epub文件。
参数:
buffer: 包含epub文件内容的Buffer对象
返回:
EpubFile: 解析后的epub对象
示例:
const buffer = fs.readFileSync("./example.epub");
const epubFile = epub.readBuffer(buffer);
EpubFile
EpubFile对象表示一个解析后的EPUB文档,包含文档的内容、样式和章节信息。该对象由readFile或readBuffer方法返回。
方法
| 名称 | 类型 | 参数 | 返回值 | 说明 |
|---|---|---|---|---|
slice | 方法 | size: int | array: Object | 将文档切分为结构化数据 |
text | 方法 | 无 | String | 转纯文本 |
markdown | 方法 | 无 | String | 转md文档 |
slice
将文档切分为结构化数据。
参数:
size: 每个切片的最大长度
返回:
array: Object: 对象数组,每个对象包含以下属性:id: 章节唯一标识符name: 章节标题content: 章节内容数组children: 子章节对象数组
示例:
const epubFile = epub.readFile("./example.epub");
const objects = epubFile.slice(600);
示例输出:
[{
"id": "1",
"name": "章节标题",
"content": [...],
"children": [{
"id": "1.1",
"name": "子章节标题",
"content": [...],
"children": []
}]
}]
text
示例:
const epubFile = epub.readFile("./example.epub");
const text = epubFile.text();
console.log(text);
markdown
示例:
const epubFile = epub.readFile("./example.epub");
const mdText = epubFile.markdown();
console.log(mdText);
pdf 对象
import {pdf} from "doct";
方法
| 名称 | 类型 | 参数 | 返回值 | 说明 |
|---|---|---|---|---|
readFile | 方法 | path: string | PdfFile | 从本地文件系统读取pdf文件 |
readBuffer | 方法 | buffer: Buffer | PdfFile | 从内存缓冲区读取pdf文件 |
readFile
从本地文件系统读取pdf文件。
参数:
path: pdf文件的路径
返回:
PdfFile: 解析后的pdf对象
示例:
const pdfFile = pdf.readFile("./example.pdf");
readBuffer
从内存缓冲区读取pdf文件。
参数:
buffer: 包含pdf文件内容的Buffer对象
返回:
PdfFile: 解析后的pdf对象
示例:
const buffer = fs.readFileSync("./example.pdf");
const pdfFile = pdf.readBuffer(buffer);
PdfFile
PdfFile对象表示一个解析后的PDF文档,包含文档的内容和结构化信息。该对象由readFile或readBuffer方法返回。
方法
| 名称 | 类型 | 参数 | 返回值 | 说明 |
|---|---|---|---|---|
slice | 方法 | size: int | array: Object | 将文档切分为结构化数据 |
text | 方法 | 无 | String | 转纯文本 |
slice
将文档切分为结构化数据。
参数:
size: 每个切片的最大长度
返回:
array: Object: 对象数组,每个对象包含以下属性:id: 页面或章节唯一标识符name: 页面或章节标题content: 页面或章节内容数组children: 子页面或子章节对象数组
示例:
const pdfFile = pdf.readFile("./example.pdf");
const objects = pdfFile.slice(600);
示例输出:
[{
"id": "1",
"name": "页面标题",
"content": [...],
"children": []
}]
text
示例:
const pdfFile = pdf.readFile("./example.pdf");
const text = pdfFile.text();
console.log(text);
pptx 对象
import {pptx} from "doct";
方法
| 名称 | 类型 | 参数 | 返回值 | 说明 |
|---|---|---|---|---|
readFile | 方法 | path: string | PptxFile | 从本地文件系统读取pptx文件 |
readBuffer | 方法 | buffer: Buffer | PptxFile | 从内存缓冲区读取pptx文件 |
readFile
从本地文件系统读取pptx文件。
参数:
path: pptx文件的路径
返回:
PptxFile: 解析后的pptx对象
示例:
const pptxFile = pptx.readFile("./example.pptx");
readBuffer
从内存缓冲区读取pptx文件。
参数:
buffer: 包含pptx文件内容的Buffer对象
返回:
PptxFile: 解析后的pptx对象
示例:
const buffer = fs.readFileSync("./example.pptx");
const pptxFile = pptx.readBuffer(buffer);
PptxFile
PptxFile对象表示一个解析后的PowerPoint文档,包含文档的幻灯片内容和样式信息。该对象由readFile或readBuffer方法返回。
方法
| 名称 | 类型 | 参数 | 返回值 | 说明 |
|---|---|---|---|---|
slice | 方法 | size: int | array: Object | 将文档切分为结构化数据 |
text | 方法 | 无 | String | 转纯文本 |
markdown | 方法 | 无 | String | 转md文档 |
slice
将文档切分为结构化数据。
参数:
size: 每个切片的最大长度
返回:
array: Object: 对象数组,每个对象包含以下属性:id: 幻灯片唯一标识符name: 幻灯片标题content: 幻灯片内容数组children: 子幻灯片对象数组(通常为空)
示例:
const pptxFile = pptx.readFile("./example.pptx");
const objects = pptxFile.slice(600);
示例输出:
[{
"id": "1",
"name": "幻灯片标题",
"content": [...],
"children": []
}]
text
示例:
const pptxFile = pptx.readFile("./example.pptx");
const text = pptxFile.text();
console.log(text);
markdown
示例:
const pptxFile = pptx.readFile("./example.pptx");
const mdText = pptxFile.markdown();
console.log(mdText);
CSV 对象
csv 对象用于解析和处理 CSV(逗号分隔值)文件,提供将 CSV 数据转换为数组或原始文本的功能。
方法
| 名称 | 类型 | 参数 | 返回值 | 说明 |
|---|---|---|---|---|
readFile | 方法 | path: string | CsvFile | 从本地文件系统读取 CSV 文件 |
readBuffer | 方法 | buffer: Buffer | CsvFile | 从内存缓冲区读取 CSV 数据 |
readFile
从本地文件系统读取 CSV 文件。
参数
path: string- CSV 文件的路径,必须以.csv结尾
返回值
Csv- 解析后的 CSV 对象
示例
const csvFile = csv.readFile("./data.csv");
readBuffer
从内存缓冲区读取 CSV 数据。
参数
buffer: Buffer- 包含 CSV 文件内容的 Buffer 对象
返回值
Csv- 解析后的 CSV 对象
示例
const buffer = fs.readFileSync("./data.csv");
const csvFile = csv.readBuffer(buffer);
// 或者从网络获取的数据
const response = await fetch('https://example.com/data.csv');
const arrayBuffer = await response.arrayBuffer();
const buffer = Buffer.from(arrayBuffer);
const csvFile = csv.readBuffer(buffer);
CsvFile
Csv 对象表示一个解析后的 CSV 文档,包含 CSV 数据的内容。该对象由 readFile 或 readBuffer 方法返回。
方法
| 名称 | 类型 | 参数 | 返回值 | 说明 |
|---|---|---|---|---|
array | 方法 | 无 | Array<Array<any>> | 将 CSV 数据解析为二维数组 |
text | 方法 | 无 | string | 获取 CSV 原始文本内容 |
array
将 CSV 数据解析为二维数组,每行是一个子数组,每个单元格是字符串值。
返回值
Array<Array<any>>- 二维数组,结构如下:
[
["Header1", "Header2", "Header3"], // 表头行
["Value1", "Value2", "Value3"], // 数据行
["Value4", "Value5", "Value6"] // 数据行
]
示例
const csvFile = csv.readFile("./data.csv");
const dataArray = csvFile.array();
// 遍历数据
dataArray.forEach((row, rowIndex) => {
console.log(`第 ${rowIndex + 1} 行:`, row.join(', '));
});
// 获取表头
const headers = dataArray[0];
console.log('表头:', headers);
// 获取数据行(排除表头)
const dataRows = dataArray.slice(1);
console.log('数据行数:', dataRows.length);
特性
- 自动处理 UTF-8 BOM(字节顺序标记)
- 支持变长行(不同行的列数可以不同)
- 宽松的引号处理,容忍不规范的 CSV 格式
- 空输入返回空数组而不是 null
text
获取 CSV 文件的原始文本内容。
返回值
string- CSV 文件的原始文本内容
示例
const csvFile = csv.readFile("./data.csv");
const rawText = csvFile.text();
console.log('原始CSV内容:');
console.log(rawText);
使用示例
示例 1:基本使用
import {csv} from "doct";
// 读取 CSV 文件
const csvFile = csv.readFile("./sales_data.csv");
// 转换为数组进行分析
const data = csvFile.array();
// 打印表头
console.log("表头:", data[0]);
// 打印前5行数据
for (let i = 1; i < Math.min(6, data.length); i++) {
console.log(`行 ${i}:`, data[i]);
}
// 获取原始文本
const rawContent = csvFile.text();
console.log("文件大小:", rawContent.length, "字符");
示例 2:数据处理
import {csv} from "doct";
const csvFile = csv.readFile("./employees.csv");
const data = csvFile.array();
// 假设 CSV 结构: [姓名, 部门, 工资]
const employees = data.slice(1).map(row => ({
name: row[0],
department: row[1],
salary: parseFloat(row[2])
}));
// 计算平均工资
const totalSalary = employees.reduce((sum, emp) => sum + emp.salary, 0);
const averageSalary = totalSalary / employees.length;
console.log(`员工数量: ${employees.length}`);
console.log(`平均工资: ${averageSalary.toFixed(2)}`);
// 按部门分组
const byDepartment = {};
employees.forEach(emp => {
if (!byDepartment[emp.department]) {
byDepartment[emp.department] = [];
}
byDepartment[emp.department].push(emp);
});
console.log('按部门分组:', byDepartment);
示例 3:错误处理
import {csv} from "doct";
try {
// 尝试读取不存在的文件
const csvFile = csv.readFile("./nonexistent.csv");
const data = csvFile.array();
} catch (error) {
console.error('读取文件失败:', error.message);
}
try {
// 尝试读取非CSV文件
const csvFile = csv.readFile("./document.txt");
} catch (error) {
console.error('文件格式错误:', error.message);
// 输出: 不支持的文件格式
}
// 处理空文件
const emptyBuffer = Buffer.from('');
const emptyCsv = csv.readBuffer(emptyBuffer);
const emptyData = emptyCsv.array();
console.log('空文件数据:', emptyData); // 输出: []
示例 4:从网络数据创建
import {csv} from "doct";
// 模拟从网络获取的 CSV 数据
const networkData = `姓名,年龄,城市
张三,25,北京
李四,30,上海
王五,28,广州`;
const buffer = Buffer.from(networkData, 'utf-8');
const csvFile = csv.readBuffer(buffer);
const data = csvFile.array();
console.log('网络CSV数据:');
data.forEach(row => {
console.log(row.join(' | '));
});
