MCPcopy Index your code
hub / github.com/InfinitiBit/graphbit / extract_docx_content

Method extract_docx_content

core/src/document_loader.rs:709–752  ·  view source on GitHub ↗

Extract content from DOCX files

(file_path: &str)

Source from the content-addressed store, hash-verified

707
708 /// Extract content from DOCX files
709 async fn extract_docx_content(file_path: &str) -> GraphBitResult<String> {
710 use std::fs::File;
711 use std::io::Read;
712
713 let mut file = File::open(file_path).map_err(|e| {
714 GraphBitError::validation("document_loader", format!("Failed to open DOCX file: {e}"))
715 })?;
716
717 let mut buffer = Vec::new();
718 file.read_to_end(&mut buffer).map_err(|e| {
719 GraphBitError::validation("document_loader", format!("Failed to read DOCX file: {e}"))
720 })?;
721
722 let docx = docx_rs::read_docx(&buffer).map_err(|e| {
723 GraphBitError::validation("document_loader", format!("Failed to parse DOCX file: {e}"))
724 })?;
725
726 let mut text_content = String::new();
727
728 // Extract text from document children
729 for child in &docx.document.children {
730 if let docx_rs::DocumentChild::Paragraph(paragraph) = child {
731 for para_child in &paragraph.children {
732 if let docx_rs::ParagraphChild::Run(run_element) = para_child {
733 for run_child in &run_element.children {
734 if let docx_rs::RunChild::Text(text) = run_child {
735 text_content.push_str(&text.text);
736 }
737 }
738 }
739 }
740 text_content.push('\n');
741 }
742 }
743
744 if text_content.trim().is_empty() {
745 return Err(GraphBitError::validation(
746 "document_loader",
747 "No text content could be extracted from the DOCX file",
748 ));
749 }
750
751 Ok(text_content.trim().to_string())
752 }
753
754 /// Extract content from Excel (XLSB, XLSX, XLS,etc.) files
755 async fn extract_excel_content(file_path: &str) -> GraphBitResult<String> {

Callers

nothing calls this directly

Calls 2

to_stringMethod · 0.80
is_emptyMethod · 0.45

Tested by

no test coverage detected