Search and replace keyword in a hyperlink and URL
To search for hyperlinks or URLs that contain a specific keyword and replace that keyword in the hyperlink and URL using Aspose.Words for Java, you can follow the example below. This code demonstrates how to find all hyperlinks in a Word document and modify their target URLs and display names based on a specified keyword.
Example Code
import com.aspose.words.*;
import org.testng.annotations.Test;
@Test
public class ReplaceHyperlinks {
@Test
public void replaceKeywordInHyperlinks() throws Exception {
Document doc = new Document("path/to/your/document.docx");
// Define the keyword to search for
String keyword = "oldKeyword";
String newKeyword = "newKeyword";
// Use XPath to find all hyperlink fields in the document
NodeList fieldStarts = doc.selectNodes("//FieldStart");
for (FieldStart fieldStart : (Iterable<FieldStart>) fieldStarts) {
if (fieldStart.getFieldType() == FieldType.FIELD_HYPERLINK) {
Hyperlink hyperlink = new Hyperlink(fieldStart);
// Check if the hyperlink is not local (i.e., it has a URL)
if (!hyperlink.isLocal()) {
// Replace the keyword in the hyperlink's target URL
String targetUrl = hyperlink.getTarget();
if (targetUrl.contains(keyword)) {
hyperlink.setTarget(targetUrl.replace(keyword, newKeyword));
}
// Replace the keyword in the hyperlink's display name
String displayName = hyperlink.getName();
if (displayName.contains(keyword)) {
hyperlink.setName(displayName.replace(keyword, newKeyword));
}
}
}
}
// Save the modified document
doc.save("path/to/your/modified_document.docx");
}
}
Explanation
- Document Loading: The document is loaded from a specified path.
- Keyword Definition: Define the keywords for searching and replacing.
- XPath Selection: Use XPath to select all hyperlink fields in the document.
- Hyperlink Processing: For each hyperlink, check if it is not local (i.e., it has a URL). If it contains the specified keyword, replace it with the new keyword in both the target URL and the display name.
- Document Saving: Finally, save the modified document to a new file.
This approach allows you to effectively search and replace keywords in hyperlinks within a Word document using Aspose.Words for Java.
For more details, you can refer to the Aspose.Words for Java documentation.
I need something for python
@Mark028 You can use the following code to achieve what you need:
doc = aw.Document("C:\\Temp\\in.docx")
for f in doc.range.fields:
if f.type == aw.fields.FieldType.FIELD_HYPERLINK:
f = f.as_field_hyperlink()
f.address = f.address.replace("PLACEHOLDER", "replacement")
doc.save("C:\\Temp\\out.docx")