Hi, I have a word which contains many math equations, such as mathtype, MS-equations, and MS-field equations. How can I get all equations and convert them all to ms-equations? Thanks.
@ZZZ21321 You can easily convert EQ fields (MS-field equations) to OfficeMath
using Aspose.Words. Fie example see the following C# code:
Document doc = new Document(@"C:\Temp\in.docx");
// Get all EQ Fields in the document.
List<FieldEQ> eqFields = doc.Range.Fields
.Where(f => f.Type == FieldType.FieldEquation).Cast<FieldEQ>().ToList();
// Conert EQ fields to OfficeMath
foreach (FieldEQ eq in eqFields)
{
OfficeMath math = eq.AsOfficeMath();
eq.End.ParentNode.InsertAfter(math, eq.End);
eq.Remove();
}
doc.Save(@"C:\Temp\out.docx");
To get all OfficeMath
you can use the following code:
NodeCollection officeMaths = doc.GetChildNodes(NodeType.OfficeMath, true);
Regarding mathtype equations, if I understand you properly, you mean embedded OLE objects, the legacy MS Word equations. Unfortunately, there is no way to convert them to OfficeMath using Aspose.Words.
@alexey.noskov Thank you for your replay. You are very nice.
So there is no way to convert mathtype to officemath in aspose.words. Thanks. but for the EQ fields equations, how about the python code? Thanks
@ZZZ21321 Here is Python version of the code:
doc = aw.Document("C:\\Temp\\in.docx")
# Get all EQ Fields in the document and convert them to OfficeMath.
for f in doc.range.fields :
if f.type == aw.fields.FieldType.FIELD_EQUATION :
eq = f.as_field_eq()
math = eq.as_office_math()
eq.end.parent_node.insert_after(math, eq.end)
eq.remove()
doc.save("C:\\Temp\\out.docx");
Could you please also attach a sample document with mathtype equations here to make sure we are talking about the same thing.
Thanks, But how can I get all office math and remove fonts, color, font-size etc style on them?
I also have a quesiton. I want to convert all non-chinese chars in word into officemath. is it possible in asponse? Thanks. any demo code?
@ZZZ21321 You can use code like this to clear formatting of office math:
doc = aw.Document("C:\\Temp\\in.docx")
officeMaths = doc.get_child_nodes(aw.NodeType.OFFICE_MATH, True)
for m in officeMaths :
math = m.as_office_math()
for r in math.get_child_nodes(aw.NodeType.RUN, True) :
r.as_run().font.clear_formatting()
doc.save("C:\\Temp\\out.docx");
You can also use Run.text property to get/set/replace text in the Run.