Skip to content
TheChatArchiveThe Chat Archive
gpt-4-0314· OpenAIAi2 WildChat-4.8M (AI2)

List<String> getAllText() { final regex = RegExp(r'<w:tbl>.*<\/w:tbl>'); final textNodes…

  1. User

    List getAllText() { final regex = RegExp(r'<w:tbl>.*</w:tbl>'); final textNodes = _document.findAllElements('w:t'); final textList = [];

    for (final node in textNodes) {
      final text = node.innerText ?? '';
      if (!regex.hasMatch(text)) {
        textList.add(text);
      }
    }
    return textList;
    

    } i want exclude table in XML from docx zip in flutter

  2. Assistant

    To exclude tables from the extracted text in a .docx file in Flutter, you can modify the getAllText() function as follows:

    1. First, read the file as a Uint8List and unzip its content using the archive package.
    2. Next, extract the word/document.xml data and parse it with the xml package.
    3. Finally, build a list of all <w:t> elements and exclude any element that is a descendant of a <w:tbl> element.

    Here's the complete modified code:

    import 'dart:io';
    import 'dart:typed_data';
    import 'package:archive/archive.dart';
    import 'package:archive/archive_io.dart';
    import 'package:xml/xml.dart';
    
    Future<List<String>> getAllText(File file) async {
      // Read file as bytes
      Uint8List fileBytes = await file.readAsBytes();
    
      // Unzip the .docx file
      Archive archive = ZipDecoder().decodeBytes(fileBytes);
    
      // Find the word/document.xml file
      ArchiveFile documentXmlFile = archive.firstWhere(
        (element) => element.name == 'word/document.xml',
      );
    
      // Decode and parse the document.xml content
      XmlDocument document = XmlDocument.parse(
        String.fromCharCodes(documentXmlFile.content),
      );
    
      // Find all <w:t> elements
      List<XmlElement> textNodes = document.findAllElements('w:t').toList();
      List<String> textList = <String>[];
    
      for (XmlElement node in textNodes) {
        final text = node.innerText ?? '';
    
        // Ignore node if it's inside a <w:tbl> element
        bool isInsideTable = node
            .ancestors
            .any((ancestor) => ancestor.name.qualified == 'w:tbl');
        if (!isInsideTable) {
          textList.add(text);
        }
      }
    
      return textList;
    }
    

    Now the getAllText() function should return all the text from the .docx file without any table text. Just pass your File object (from any file picker) to this function.