您可以在云端以编程方式轻松编辑您的 Word 文档。您可以添加、编辑文档的内容,或者您可以在 Node.js 应用程序中以编程方式应用 Word 文档中的文本格式。本文将重点介绍如何在 Node.js 中使用 REST API 编辑 Word 文档。
本文应涵盖以下主题:
- 文档编辑器 REST API 和 Node.js SDK
- 在 Node.js 中使用 REST API 编辑 Word 文档
- 使用 Node.js 在 Word 文档中添加表格
- 使用 Node.js 在 Word 文档中插入图像
文档编辑器 REST API 和 Node.js SDK
我将使用 GroupDocs.Editor Cloud 的 Node.js SDK API 来编辑 DOCX 文件。它允许您以编程方式编辑支持格式的文档,例如 Word、Excel 电子表格、PowerPoint, TXT、HTML、XML。 API 还使您能够将文档转换为 HTML 以进行编辑,并在文档编辑后将其转换回保持相同外观的原始格式。它还为云 API 提供 .NET、Java、PHP、Ruby、Android 和 Python SDK 作为其文档编辑器家族成员。
您可以在控制台中使用以下命令将 GroupDocs.Editor Cloud 安装到您的 Node.js 应用程序:
npm install groupdocs-editor-cloud
在执行上述步骤之前,请从 dashboard 获取您的 Client ID 和 Secret。获得 ID 和密码后,添加如下所示的代码:
global.clientId = "112f0f38-9dae-42d5-b4fc-cc84ae644972";
global.clientSecret = "16ad3fe0bdc39c910f57d2fd48a5d618";
global.myStorage = "";
const configuration = new groupdocs_editor_cloud.Configuration(clientId, clientSecret);
configuration.apiBaseUrl = "https://api.groupdocs.cloud";
在 Node.js 中使用 REST API 编辑 Word 文档
您可以按照以下简单步骤编辑 Word 文档:
上传文件
首先,使用下面给出的代码示例将 Word 文档 (DOCX) 上传到云端:
// 构造FileApi
let fileApi = groupdocs_editor_cloud.FileApi.fromConfig(configuration);
let resourcesFolder = 'C:\\Files\\sample.docx';
fs.readFile(resourcesFolder, (err, fileStream) => {
let request = new groupdocs_editor_cloud.UploadFileRequest("sample.docx", fileStream, myStorage);
fileApi.uploadFile(request);
});
因此,上传的 DOCX 文件将在云端仪表板的 文件部分 中可用。
使用 Node.js 编辑 Word 文档
请按照下面提到的步骤以编程方式编辑 Word 文档。
- 创建 FileAPI 和 [EditAPI] 的实例(https://apireference.groupdocs.cloud/editor/#/Edit)
- 在 FileInfo 中提供输入文件路径
- 创建 WordProcessingLoadOptions
- 使用 LoadOptions 创建 LoadRequest
- 使用 EditAPI 的 load() 方法加载文件
- 使用加载的文件创建 DownloadFileRequest
- 使用 FileAPI 的 downloadFile() 方法下载 HTML 文档
- 编辑下载的 HTML 文档
- 创建上传文件请求
- 使用 FileAPI 的 uploadFile() 方法上传 HTML
- 提供 WordProcessingSaveOptions 以保存在 DOCX 中
- 使用 SaveOptions 创建 SaveRequest
- 使用 Edit API 的 save() 方法将 HTML 保存回 DOCX
以下代码片段显示了如何在 Node.js 中使用 REST API 编辑 Word 文档。
// 接口初始化
let editApi = groupdocs_editor_cloud.EditApi.fromKeys(clientId, clientSecret);
let fileApi = groupdocs_editor_cloud.FileApi.fromKeys(clientId, clientSecret);
// 输入文件
let fileInfo = new groupdocs_editor_cloud.FileInfo();
fileInfo.filePath = "Sample.docx";
// 定义加载选项
let loadOptions = new groupdocs_editor_cloud.WordProcessingLoadOptions();
loadOptions.fileInfo = fileInfo;
loadOptions.outputPath = "output";
// 创建加载请求
let loadRequest = new groupdocs_editor_cloud.LoadRequest(loadOptions);
let loadResult = await editApi.load(loadRequest);
// 下载html文件
let downloadRequest = new groupdocs_editor_cloud.DownloadFileRequest(loadResult.htmlPath);
let buf = await fileApi.downloadFile(downloadRequest);
let htmlString = buf.toString("utf-8");
// 编辑一些东西...
htmlString = htmlString.replace("Title of the document", "Welcome");
htmlString = htmlString.replace("Subtitle #1", "Hello world");
// 将 html 上传回存储
let uploadRequest = new groupdocs_editor_cloud.UploadFileRequest(loadResult.htmlPath, new Buffer.from(htmlString, "utf-8"));
await fileApi.uploadFile(uploadRequest);
// 将 html 保存回 docx
let saveOptions = new groupdocs_editor_cloud.WordProcessingSaveOptions();
saveOptions.fileInfo = fileInfo;
saveOptions.outputPath = "output/edited.docx";
saveOptions.htmlPath = loadResult.htmlPath;
saveOptions.resourcesPath = loadResult.resourcesPath;
// 创建保存请求
let saveRequest = new groupdocs_editor_cloud.SaveRequest(saveOptions);
let saveResult = await editApi.save(saveRequest);
console.log("Document edited: " + saveResult.path);
下载更新文件
以上代码示例将编辑后的 Word 文档 (DOCX) 保存在云端。您可以使用以下代码示例下载它:
// 初始化接口
var fileApi = groupdocs_editor_cloud.FileApi.fromConfig(configuration);
// 创建文件下载请求
let request = new groupdocs_editor_cloud.DownloadFileRequest("output/edited.docx", myStorage);
// 下载文件
let response = await fileApi.downloadFile(request);
// 将图像文件保存在工作目录中
fs.writeFile("C:\\Files\\edited.docx", response, "binary", function (err) { });
使用 Node.js 在 Word 文档中添加表格
您可以按照以下步骤以编程方式在 Word 文档中添加表格:
- 创建 FileAPI 和 [EditAPI] 的实例(https://apireference.groupdocs.cloud/editor/#/Edit)
- 在 FileInfo 中提供输入文件路径
- 创建 WordProcessingLoadOptions
- 使用 LoadOptions 创建 LoadRequest
- 使用 EditAPI 的 load() 方法加载文件
- 使用加载的文件创建 DownloadFileRequest
- 使用 FileAPI 的 downloadFile() 方法下载 HTML 文档
- 编辑下载的 HTML 文档 and add a table
- 创建上传文件请求
- 使用 FileAPI 的 uploadFile() 方法上传 HTML
- 提供 WordProcessingSaveOptions 以保存在 DOCX 中
- 使用 SaveOptions 创建 SaveRequest
- 使用 EditAPI 的 save() 方法将 HTML 保存回 DOCX
以下代码片段显示了如何在 Node.js 中使用 REST API 在 Word 文档中添加表格。
// 接口初始化
let editApi = groupdocs_editor_cloud.EditApi.fromKeys(clientId, clientSecret);
let fileApi = groupdocs_editor_cloud.FileApi.fromKeys(clientId, clientSecret);
// 输入文件
let fileInfo = new groupdocs_editor_cloud.FileInfo();
fileInfo.filePath = "Sample.docx";
// 定义加载选项
let loadOptions = new groupdocs_editor_cloud.WordProcessingLoadOptions();
loadOptions.fileInfo = fileInfo;
loadOptions.outputPath = "output";
// 创建加载请求
let loadRequest = new groupdocs_editor_cloud.LoadRequest(loadOptions);
let loadResult = await editApi.load(loadRequest);
// 下载html文件
let downloadRequest = new groupdocs_editor_cloud.DownloadFileRequest(loadResult.htmlPath);
let buf = await fileApi.downloadFile(downloadRequest);
let htmlString = buf.toString("utf-8");
// 添加表
htmlString = htmlString.replace("left-aligned.", "left-aligned. <br/><table style=\"width: 100%;background-color: #dddddd;\">"
+ "<caption style=\"font-weight:bold;\"> Persons List</caption>"
+ "<tr><th>First Name</th><th>Last Name</th><th>Age</th></tr>"
+ "<tr><td>Jill</td><td>Smith</td><td>50</td></tr>"
+ "<tr><td>Eve</td><td>Jackson</td><td>94</td></tr>"
+ "</table>");
// 将 html 上传回存储
let uploadRequest = new groupdocs_editor_cloud.UploadFileRequest(loadResult.htmlPath, new Buffer.from(htmlString, "utf-8"));
await fileApi.uploadFile(uploadRequest);
// 将 html 保存回 docx
let saveOptions = new groupdocs_editor_cloud.WordProcessingSaveOptions();
saveOptions.fileInfo = fileInfo;
saveOptions.outputPath = "output/edited.docx";
saveOptions.htmlPath = loadResult.htmlPath;
saveOptions.resourcesPath = loadResult.resourcesPath;
// 创建保存请求
let saveRequest = new groupdocs_editor_cloud.SaveRequest(saveOptions);
let saveResult = await editApi.save(saveRequest);
console.log("Document edited: " + saveResult.path);
使用 Node.js 在 Word 文档中插入图像
您可以按照以下步骤以编程方式在 Word 文档中插入图像:
- 创建 FileAPI 和 [EditAPI] 的实例(https://apireference.groupdocs.cloud/editor/#/Edit)
- 在 FileInfo 中提供输入文件路径
- 创建 WordProcessingLoadOptions
- 使用 LoadOptions 创建 LoadRequest
- 使用 EditAPI 的 load() 方法加载文件
- 使用加载的文件创建 DownloadFileRequest
- 使用 FileAPI 的 downloadFile() 方法下载 HTML 文档
- 编辑下载的 HTML 文档 and insert an image
- 创建上传文件请求
- 使用 FileAPI 的 uploadFile() 方法上传 HTML
- 提供 WordProcessingSaveOptions 以保存在 DOCX 中
- 使用 SaveOptions 创建 SaveRequest
- 使用 EditAPI 的 save() 方法将 HTML 保存回 DOCX
以下代码片段显示了如何在 Node.js 中使用 REST API 在 Word 文档中插入图像。
// 接口初始化
let editApi = groupdocs_editor_cloud.EditApi.fromKeys(clientId, clientSecret);
let fileApi = groupdocs_editor_cloud.FileApi.fromKeys(clientId, clientSecret);
// 输入文件
let fileInfo = new groupdocs_editor_cloud.FileInfo();
fileInfo.filePath = "Sample.docx";
// 定义加载选项
let loadOptions = new groupdocs_editor_cloud.WordProcessingLoadOptions();
loadOptions.fileInfo = fileInfo;
loadOptions.outputPath = "output";
// 创建加载请求
let loadRequest = new groupdocs_editor_cloud.LoadRequest(loadOptions);
let loadResult = await editApi.load(loadRequest);
// 下载html文件
let downloadRequest = new groupdocs_editor_cloud.DownloadFileRequest(loadResult.htmlPath);
let buf = await fileApi.downloadFile(downloadRequest);
let htmlString = buf.toString("utf-8");
// 插入图像
htmlString = htmlString.replace("left-aligned.", "left-aligned. <br/> <img src=\"sample.png\" alt=\"signatures\" style=\"width: 128px; height: 128px;\">");
// 将 html 上传回存储
let uploadRequest = new groupdocs_editor_cloud.UploadFileRequest(loadResult.htmlPath, new Buffer.from(htmlString, "utf-8"));
await fileApi.uploadFile(uploadRequest);
// 将 html 保存回 docx
let saveOptions = new groupdocs_editor_cloud.WordProcessingSaveOptions();
saveOptions.fileInfo = fileInfo;
saveOptions.outputPath = "output/edited.docx";
saveOptions.htmlPath = loadResult.htmlPath;
saveOptions.resourcesPath = loadResult.resourcesPath;
// 创建保存请求
let saveRequest = new groupdocs_editor_cloud.SaveRequest(saveOptions);
let saveResult = await editApi.save(saveRequest);
console.log("Document edited: " + saveResult.path);
在线试用
请试用以下使用上述API开发的免费在线DOCX编辑工具。 https://products.groupdocs.app/editor/docx
结论
在本文中,您了解了如何在云端编辑 Word 文档。您还学习了如何使用 Node.js 中的 REST API 在 DOCX 文件中添加表格。此外,您还学习了如何以编程方式在 Word 文档中插入图像。本文还介绍了如何以编程方式将 DOCX 文件上传到云端,然后从云端下载编辑后的文件。您可以使用 文档 了解有关 GroupDocs.Editor Cloud API 的更多信息。我们还提供了一个 API 参考 部分,让您可以直接通过浏览器可视化我们的 API 并与之交互。如有任何歧义,请随时在论坛上与我们联系。