gpt-4-0314· OpenAI
WildChat-4.8M (AI2)
List<String> getAllText() { final regex = RegExp(r'<w:tbl>.*<\/w:tbl>'); final textNodes…
- 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
- Assistant
To exclude tables from the extracted text in a
.docxfile in Flutter, you can modify thegetAllText()function as follows:- First, read the file as a
Uint8Listand unzip its content using thearchivepackage. - Next, extract the
word/document.xmldata and parse it with thexmlpackage. - 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.docxfile without any table text. Just pass your File object (from any file picker) to this function. - First, read the file as a