求Aspose Word Cloud API(NodeJS/Python)的文本替换完整运行示例
Absolutely! I’ve got you covered with working examples for both Node.js and Python that walk through your exact workflow—uploading a DOCX file containing "Hello World", replacing the text with "How are you, Universe?", and downloading the modified version using Aspose.Words Cloud API. Here’s how to implement it:
First, install the official Aspose.Words Cloud SDK for Node.js:
npm install asposewordscloud
Then use this code snippet, making sure to replace YOUR_CLIENT_ID and YOUR_CLIENT_SECRET with your actual credentials from the Aspose Cloud dashboard:
const { WordsApi, UploadFileRequest, ReplaceTextRequest, DownloadFileRequest } = require('asposewordscloud'); const fs = require('fs'); // Initialize the API client const wordsApi = new WordsApi("YOUR_CLIENT_ID", "YOUR_CLIENT_SECRET"); async function modifyDocx() { try { // Step 1: Upload the source DOCX file to Aspose Cloud storage const uploadRequest = new UploadFileRequest({ path: "HelloWorld.docx", fileContent: fs.createReadStream("./HelloWorld.docx") }); await wordsApi.uploadFile(uploadRequest); console.log("File uploaded successfully"); // Step 2: Replace the target text in the document const replaceRequest = new ReplaceTextRequest({ name: "HelloWorld.docx", oldValue: "Hello World", newValue: "How are you, Universe?", isMatchCase: true, isMatchWholeWord: true }); await wordsApi.replaceText(replaceRequest); console.log("Text replaced successfully"); // Step 3: Download the modified document const downloadRequest = new DownloadFileRequest({ path: "HelloWorld.docx" }); const downloadResult = await wordsApi.downloadFile(downloadRequest); fs.writeFileSync("./ModifiedHelloWorld.docx", downloadResult.body); console.log("Modified file downloaded successfully"); } catch (error) { console.error("Error occurred:", error.message); } } // Run the workflow modifyDocx();
Start by installing the Python SDK:
pip install aspose-words-cloud
Use this code, again replacing YOUR_CLIENT_ID and YOUR_CLIENT_SECRET with your own credentials:
import asposewordscloud from asposewordscloud.models import ReplaceTextRequest from asposewordscloud.apis import WordsApi import os # Initialize the API client words_api = WordsApi(client_id='YOUR_CLIENT_ID', client_secret='YOUR_CLIENT_SECRET') def modify_docx(): try: # Step 1: Upload the source DOCX file upload_file_path = os.path.join(os.getcwd(), "HelloWorld.docx") with open(upload_file_path, 'rb') as file: upload_request = asposewordscloud.UploadFileRequest( path="HelloWorld.docx", file_content=file ) words_api.upload_file(upload_request) print("File uploaded successfully") # Step 2: Replace the text in the document replace_request = ReplaceTextRequest( name="HelloWorld.docx", old_value="Hello World", new_value="How are you, Universe?", is_match_case=True, is_match_whole_word=True ) words_api.replace_text(replace_request) print("Text replaced successfully") # Step 3: Download the modified document download_request = asposewordscloud.DownloadFileRequest( path="HelloWorld.docx" ) download_result = words_api.download_file(download_request) with open("ModifiedHelloWorld.docx", 'wb') as output_file: output_file.write(download_result) print("Modified file downloaded successfully") except Exception as e: print(f"Error occurred: {str(e)}") # Execute the workflow if __name__ == '__main__': modify_docx()
Quick Notes:
- Ensure you have a
HelloWorld.docxfile in the same directory as your script, containing the exact text "Hello World". - You can get your Client ID and Client Secret by signing up for an Aspose Cloud account and creating an application in the dashboard.
内容的提问来源于stack exchange,提问作者Ruel Abion

