Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

Gemini api to summary content from docx

I am new to Gemini. In the system it will read the docx by Tag ,  so that the content will involve  characters and tables  , for these contents will be xml format in docx ,or i can directly get all the inner text but the tables and its data may be unreadable , now i want to use gemini-1.5-flash-001 to summarize the content , how can i handle the xml format contents include the tables ? 

0 2 1,045
2 REPLIES 2

Hi @nnnnanyu ,

To handle .docx files in XML format and summarize their content, including characters and tables, using Gemini-1.5-Flash-001, you need a systematic approach that processes both text and structured data like tables effectively. Here’s how you can manage this:

1. Extract and Parse .docx File Content

.docx files are essentially zipped archives containing XML files. You can extract and parse the content using Python libraries like python-docx or zipfile and xml.etree.ElementTree.

Option 1: Use python-docx for easier extraction and parsing.

  • Install python-docx if you haven’t already:
    bash
     
    pip install python-docx
  • Extract text and tables using this library:
    Python
    from docx import Document # Load the .docx file doc = Document('your_file.docx') # Extract paragraphs (text content) text_content = [para.text for para in doc.paragraphs] # Extract tables tables_data = [] for table in doc.tables: table_data = [] for row in table.rows: row_data = [cell.text for cell in row.cells] table_data.append(row_data) tables_data.append(table_data) # Combine the text and table data if needed combined_content = ' '.join(text_content) + ' '.join([' '.join(row) for table in tables_data for row in table])

Option 2: Handle .docx as an XML file for more control over elements.

  • Unzip the .docx file manually or programmatically using zipfile and parse word/document.xml using xml.etree.ElementTree to access raw XML structure for text and tables.

2. Preprocess Content for Summarization

Once you’ve extracted text content and tables:

  • Normalize text: Remove or handle special characters, whitespace, and formatting.
  • Convert tables to structured text: Tables can be represented as bullet points, key-value pairs, or structured text before summarization.
  • Combine text and table data: If tables contain relevant data, ensure they are summarized meaningfully. Convert tables into plain text summaries or bullet lists.

3. Summarize Content with Gemini-1.5-Flash-001

To use Gemini-1.5-Flash-001 for summarization, you'll need to provide input in a clear, structured text format. If the model you’re using is an API-based LLM, you can prepare and feed the input as follows:

Example using a summary prompt:

 

python
# Assuming you have a function 'summarize_with_gemini' to call Gemini-1.5-Flash-001 API or similar model text_input = combined_content # From the previous step summary = summarize_with_gemini(input_text=text_input) print(summary)
 

4. Handling Tables in the Summarization

If the summarization model doesn’t inherently handle table data well:

  • Preprocess tables into text summaries: Convert each table row into descriptive text (e.g., "Row 1: Column1 = Value1, Column2 = Value2").
  • Separate summaries: Summarize text content and table content separately if necessary, then combine the outputs.

Tips for Effective Summarization:

  1. Break down content: If your .docx file is large, divide the content into manageable chunks.
  2. Maintain context: Provide enough context around tables to make the summarization coherent.
  3. Fine-tune prompts: Craft prompts for the LLM that clearly instruct how to handle table data (e.g., "Summarize the following content including this table data...").

If you have any question , please let me know 

 

Thanks ! Could you please advice a give  good  prompts for summary paragraphs and the table contend and also image data