Extract relevant OOXML for a chart using Aspose.Words

Hi, I wanted to extract the following OOXML from a word doc, this word doc was created by by saving an excel workbook as a Docx with SaveAsEditableShapes marked as true. I will attach the OOXML data I require. The idea is to insert a chart into a word doc using OOXML data.
The chart along with markup required:
ChartWithRequiredOOXML.zip (33.7 KB)

The Chart’s complete OOXML with actual data.
InsertedChartXMLWithBinaryData.zip (14.2 KB)

here are all the different package parts I require:
pkg:name=“/_rels/.rels”
pkg:name=“/word/_rels/document.xml.rels”
pkg:name=“/word/document.xml”
pkg:name=“/word/charts/chart1.xml”
pkg:name=“/word/charts/_rels/chart1.xml.rels”
pkg:name=“/word/embeddings/Microsoft_Excel_Worksheet.xlsx”

@Adhirath

To extract the relevant OOXML for a chart from a Word document using Aspose.Words, follow these steps:

  1. Cause: You need to extract the OOXML representation of a chart from a Word document for further processing or insertion into another document.

  2. Solution:

    • Load the Document: Start by loading your Word document that contains the chart.
    • Access the Chart: Navigate to the chart within the document by iterating through the document’s shapes and checking if they are charts.
    • Extract OOXML: Once you have access to the chart, use the GetChartData method to extract its OOXML representation.

Here’s a sample code snippet to help you get started:

using Aspose.Words;
using Aspose.Words.Drawing;

Document doc = new Document("path/to/your/document.docx");
foreach (Shape shape in doc.GetChildNodes(NodeType.Shape, true))
{
    if (shape.Chart != null)
    {
        // Extract the OOXML for the chart
        string chartOOXML = shape.Chart.ToString(SaveFormat.Ooxml);
        // Save or process the OOXML as needed
        Console.WriteLine(chartOOXML);
    }
}
  1. Next Steps:

    • After extracting the OOXML, save the necessary parts (like document.xml, chart1.xml, etc.) to their respective files.
    • If your goal is to insert this chart into another Word document, use the InsertChart method to add the chart back into the new document.
  2. Closing Line: Ensure to adjust the paths and handle any exceptions as needed. If you have any further questions or need additional assistance with this process, feel free to ask!

@Adhirath There is no way to extract pure OOXML of particular element in the document. Aspose.Words does not keep original OOXML, whole document is read into Aspose.Words DOM.

If you need to copy chart from one document to another, just import the chart shape node and insert into another document.
https://reference.aspose.com/words/net/aspose.words/nodeimporter/importnode/

The XML you have provided is MS Word 2007 XML. You can copy the chart into a separate document and save this document to FlatOpc format to get such XML.

So what I am hearing is once I have saved an xlsx as docx and have an editable chart in it I should simply copy this chart from one doc to another(keep in mind my intial docx which was converted from xlsx will also have only 1 chart) and then save it as flatOPC?

@Adhirath If there is only one chart in the source document. There is no need to copy it into a separate document. You can simply save DOCX as FlatOpc.

1 Like