您可以在雲端以編程方式輕鬆編輯您的 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 並與之交互。如有任何歧義,請隨時在論壇上與我們聯繫。