### Install oxmltotext Source: https://github.com/young2j/oxmltotext/blob/master/README.md Use the go get command to add the library to your project. ```shell go get -u github.com/young2j/oxmltotext@latest ``` -------------------------------- ### Run benchmarks Source: https://github.com/young2j/oxmltotext/blob/master/README.md Commands to start the Tika server and execute the benchmark suite. ```shell cd tikaserver java -jar tika-server-standard-2.9.1.jar cd ../benchmark make bench_all ``` -------------------------------- ### Install Tesseract OCR dependencies Source: https://github.com/young2j/oxmltotext/blob/master/README.md Commands to install Tesseract OCR and language packages on Debian-based systems or macOS. ```shell apt install -y --no-install-recommends libtesseract-dev # libs apt install -y --no-install-recommends tesseract-ocr-eng tesseract-ocr-chi-sim tesseract-ocr-script-hans # language packages ``` ```shell brew install tesseract brew install tesseract-lang # language packages ``` -------------------------------- ### Start Apache Tika server for PPT extraction Source: https://github.com/young2j/oxmltotext/blob/master/README.md Commands to download and run the Apache Tika server required for PPT text extraction. ```shell # see tikaserver/local.sh wget --no-check-certificate https://dlcdn.apache.org/tika/2.9.1/tika-server-standard-2.9.1.jar java -jar tika-server-standard-2.9.1.jar ``` -------------------------------- ### Install Antiword for DOC extraction Source: https://github.com/young2j/oxmltotext/blob/master/README.md Commands to install Antiword, a dependency required for processing legacy .doc files. ```shell apt install -y --no-install-recommends antiword # or on MacOS brew install antiword ``` -------------------------------- ### Extract DOC using Antiword Source: https://context7.com/young2j/oxmltotext/llms.txt Extracts text from legacy DOC files using the antiword command-line tool. Requires antiword to be installed on the system. ```go package main import ( "fmt" "github.com/young2j/oxmltotext/doctotext" ) func main() { // Requires: apt install antiword (or brew install antiword on macOS) // From file path texts, err := doctotext.ExtractFromPath("document.doc") if err != nil { panic(err) } fmt.Println(texts) // From URL texts, statusCode, err := doctotext.ExtractFromURL("https://example.com/document.doc") if err != nil { fmt.Printf("HTTP Status: %d\n", statusCode) panic(err) } fmt.Println(texts) } ``` -------------------------------- ### Extract text from DOCX with OCR Source: https://github.com/young2j/oxmltotext/blob/master/README.md Example of extracting text from a DOCX file, including enabling image parsing via OCR. ```go func main() { dp, err := docxtotext.Open("../filesamples/file-sample_100kb.docx") if err != nil { panic(err) } defer dp.Close() // Please remember to call the `Close` method to avoid memory leaks. dp.SetParseImages(true) // set true if you want to parse images text texts, err := dp.ExtractTexts() if err != nil { panic(err) } fmt.Println(texts) } ``` -------------------------------- ### Extract DOCX with OCR for Images Source: https://context7.com/young2j/oxmltotext/llms.txt Enables OCR to extract text from images embedded in a DOCX file. This requires tesseract-ocr to be installed and the Go package to be built with the 'ocr' tag. ```go package main import ( "fmt" "github.com/young2j/oxmltotext/docxtotext" ) func main() { dp, err := docxtotext.Open("document.docx") if err != nil { panic(err) } defer dp.Close() // Enable image OCR (requires tesseract-ocr installed) // Build with: go build -tags ocr . dp.SetParseImages(true) texts, err := dp.ExtractTexts() if err != nil { panic(err) } fmt.Println(texts) // Output includes OCR results: // ┌──────────────────────image──────────────────────┐ // Extracted text from image... // └─────────────────────────────────────────────────┘ } ``` -------------------------------- ### Open DOCX File from Path Source: https://context7.com/young2j/oxmltotext/llms.txt Opens a DOCX file from a local file path. Ensure to defer the Close() method to prevent memory leaks. ```go package main import ( "fmt" "github.com/young2j/oxmltotext/docxtotext" ) func main() { // Open DOCX file from path dp, err := docxtotext.Open("document.docx") if err != nil { panic(err) } defer dp.Close() // Always close to avoid memory leaks // Extract all text content texts, err := dp.ExtractTexts() if err != nil { panic(err) } fmt.Println(texts) } ``` -------------------------------- ### Open PDF File and Extract All Text Source: https://context7.com/young2j/oxmltotext/llms.txt Opens a PDF file using the go-fitz library and extracts text from all pages. Ensure the PDF file exists at the specified path. ```go package main import ( "fmt" "github.com/young2j/oxmltotext/pdftotext" ) func main() { pp, err := pdftotext.Open("document.pdf") if err != nil { panic(err) } defer pp.Close() // Get number of pages fmt.Printf("Total pages: %d\n", pp.NumPages()) // Extract all text texts, err := pp.ExtractTexts() if err != nil { panic(err) } fmt.Println(texts) } ``` -------------------------------- ### Build with OCR support Source: https://github.com/young2j/oxmltotext/blob/master/README.md Command to compile the application with the 'ocr' build tag enabled. ```shell go build -tags ocr . ``` -------------------------------- ### Open DOCX File from URL Source: https://context7.com/young2j/oxmltotext/llms.txt Opens a DOCX file directly from a URL. It returns the parser, HTTP status code, and any error encountered during the fetch. ```go package main import ( "fmt" "github.com/young2j/oxmltotext/docxtotext" ) func main() { // Open DOCX file from URL dp, statusCode, err := docxtotext.OpenURL("https://example.com/document.docx") if err != nil { fmt.Printf("HTTP Status: %d\n", statusCode) panic(err) } defer dp.Close() texts, err := dp.ExtractTexts() if err != nil { panic(err) } fmt.Println(texts) } ``` -------------------------------- ### Configure DOCX Parser Options Source: https://context7.com/young2j/oxmltotext/llms.txt Customize separators and selectively enable/disable parsing of document sections like comments, headers, and footers. Logging can also be disabled. ```go package main import ( "fmt" "github.com/young2j/oxmltotext/docxtotext" ) func main() { dp, err := docxtotext.Open("document.docx") if err != nil { panic(err) } defer dp.Close() // Customize separators dp.SetParagraphSep("\n\n") // Double newline between paragraphs dp.SetPartSep("=====") // Separator between document parts dp.SetTableRowSep("\n") // Table row separator dp.SetTableColSep(" | ") // Table column separator // Selectively parse document parts (all true by default) dp.SetParseComments(true) dp.SetParseHeaders(true) dp.SetParseFooters(true) dp.SetParseFootnotes(true) dp.SetParseEndnotes(true) // Disable logging dp.DisableLogging(true) texts, err := dp.ExtractTexts() if err != nil { panic(err) } fmt.Println(texts) } ``` -------------------------------- ### Configure PPTX Parser Options Source: https://context7.com/young2j/oxmltotext/llms.txt Customize PPTX extraction by setting separators for slides, phrases, table rows, and columns. Options to parse charts, diagrams, and images are available. Formatting borders can also be disabled. ```go package main import ( "fmt" "github.com/young2j/oxmltotext/pptxtotext" ) func main() { pp, err := pptxtotext.Open("presentation.pptx") if err != nil { panic(err) } defer pp.Close() // Customize separators pp.SetSlideSep("=== SLIDE ===") // Separator between slides pp.SetPhraseSep(" ") // Separator between text phrases pp.SetTableRowSep("\n") // Table row separator pp.SetTableColSep("\t") // Table column separator // Enable embedded content parsing pp.SetParseCharts(true) pp.SetParseDiagrams(true) pp.SetParseImages(true) // Requires OCR // Remove formatting borders pp.SetDrawingsNoFmt(true) texts, err := pp.ExtractTexts() if err != nil { panic(err) } fmt.Println(texts) } ``` -------------------------------- ### Open DOCX File from Reader Source: https://context7.com/young2j/oxmltotext/llms.txt Opens a DOCX file from an io.ReaderAt interface, suitable for in-memory data or custom sources. Requires the size of the reader. ```go package main import ( "bytes" "fmt" "os" "github.com/young2j/oxmltotext/docxtotext" ) func main() { // Read file into memory data, err := os.ReadFile("document.docx") if err != nil { panic(err) } // Create reader and open r := bytes.NewReader(data) dp, err := docxtotext.OpenReader(r, r.Size()) if err != nil { panic(err) } defer dp.Close() texts, err := dp.ExtractTexts() if err != nil { panic(err) } fmt.Println(texts) } ``` -------------------------------- ### Open PDF from URL or Reader Source: https://context7.com/young2j/oxmltotext/llms.txt Opens PDF files from a URL or an io.Reader interface. Allows customization of the page separator when extracting text. ```go package main import ( "fmt" "os" "github.com/young2j/oxmltotext/pdftotext" ) func main() { // From URL pp, statusCode, err := pdftotext.OpenURL("https://example.com/document.pdf") if err != nil { fmt.Printf("HTTP Status: %d\n", statusCode) panic(err) } defer pp.Close() // Customize page separator pp.SetPageSep("\n--- PAGE ---\n") texts, err := pp.ExtractTexts() if err != nil { panic(err) } fmt.Println(texts) // From Reader f, _ := os.Open("document.pdf") defer f.Close() pp2, err := pdftotext.OpenReader(f) if err != nil { panic(err) } defer pp2.Close() } ``` -------------------------------- ### Extract plain text from DOCX Source: https://github.com/young2j/oxmltotext/blob/master/README.md Demonstrates opening a DOCX file and extracting its text content. Ensure Close() is called to prevent memory leaks. ```go import ( "fmt" "github.com/young2j/oxmltotext/docxtotext" ) func main() { dp, err := docxtotext.Open("../filesamples/file-sample_100kb.docx") if err != nil { panic(err) } defer dp.Close() // Please remember to call the `Close` method to avoid memory leaks. texts, err := dp.ExtractTexts() if err != nil { panic(err) } fmt.Println(texts) } ``` -------------------------------- ### Configure XLSX Parser Options Source: https://context7.com/young2j/oxmltotext/llms.txt Customizes XLSX extraction with separators for sheets, rows, and columns. Options to parse charts, diagrams, and images are available, along with a setting to only extract shared strings for faster processing. ```go package main import ( "fmt" "github.com/young2j/oxmltotext/xlsxtotext" ) func main() { xp, err := xlsxtotext.Open("spreadsheet.xlsx") if err != nil { panic(err) } defer xp.Close() // Customize separators xp.SetSheetSep("---") // Separator between sheets xp.SetRowSep("\n") // Row separator xp.SetColSep(",") // Column separator (CSV-like output) // Enable chart and diagram parsing xp.SetParseCharts(true) xp.SetParseDiagrams(true) xp.SetParseImages(true) // Requires OCR // Only extract shared strings (faster, less complete) xp.SetOnlySharedStrings(false) // Disable logging xp.SetDisableLogging(true) texts, err := xp.ExtractTexts() if err != nil { panic(err) } fmt.Println(texts) } ``` -------------------------------- ### Implement Custom OCR Source: https://context7.com/young2j/oxmltotext/llms.txt Provides a custom OCR implementation to replace the default tesseract-ocr for image text extraction. Implement your OCR logic within the CustomOCR struct's Run method. ```go package main import ( "fmt" "io" "github.com/young2j/oxmltotext/docxtotext" "github.com/young2j/oxmltotext/types" ) // CustomOCR implements types.OCR interface type CustomOCR struct { // Your OCR client configuration } func (c *CustomOCR) Run(r io.Reader) (string, error) { // Implement your OCR logic here // Read image from r and return extracted text data, err := io.ReadAll(r) if err != nil { return "", err } // Call your OCR service (e.g., Google Cloud Vision, AWS Textract) result := fmt.Sprintf("OCR result for %d bytes", len(data)) return result, nil } func (c *CustomOCR) Close() error { // Cleanup resources return nil } func main() { dp, err := docxtotext.Open("document.docx") if err != nil { panic(err) } defer dp.Close() // Set custom OCR interface customOCR := &CustomOCR{} dp.SetOcrInterface(customOCR) dp.SetParseImages(true) texts, err := dp.ExtractTexts() if err != nil { panic(err) } fmt.Println(texts) } ``` -------------------------------- ### Extract charts and diagrams from DOCX Source: https://github.com/young2j/oxmltotext/blob/master/README.md Configures the extractor to include text from charts and diagrams before extracting content. ```go func main() { dp, err := docxtotext.Open("../filesamples/file-sample_100kb.docx") if err != nil { panic(err) } defer dp.Close() // Please remember to call the `Close` method to avoid memory leaks. dp.SetParseCharts(true) // set true if you want to parse charts text dp.SetParseDiagrams(true) // set true if you want to parse diagrams text texts, err := dp.ExtractTexts() if err != nil { panic(err) } fmt.Println(texts) } ``` -------------------------------- ### Compile xlstotext for XLS extraction Source: https://github.com/young2j/oxmltotext/blob/master/README.md Steps to compile the required xlstotext executable using Cargo. ```shell cd xlstotext/rs cargo build --relese # executable program: xlstotext/rs/target/release/xlstotext ``` -------------------------------- ### Extract DOC using Tika Server Source: https://context7.com/young2j/oxmltotext/llms.txt Extracts text from DOC files using an Apache Tika server. This method requires a running Tika server and provides more comprehensive extraction capabilities. ```go package main import ( "fmt" "strings" "github.com/young2j/oxmltotext/doctotext" ) func main() { // Requires Tika server running: // wget https://dlcdn.apache.org/tika/2.9.1/tika-server-standard-2.9.1.jar // java -jar tika-server-standard-2.9.1.jar tikaURL := "http://localhost:9998/tika" // From file path texts, statusCode, err := doctotext.ExtractFromPathByTika("document.doc", tikaURL) if err != nil { fmt.Printf("Tika response status: %d\n", statusCode) panic(err) } fmt.Println(texts) // From Reader reader := strings.NewReader("doc content bytes...") texts, statusCode, err = doctotext.ExtractFromReaderByTika(reader, reader.Len(), tikaURL) if err != nil { panic(err) } fmt.Println(texts) } ``` -------------------------------- ### Open PPTX File and Extract Text Source: https://context7.com/young2j/oxmltotext/llms.txt Opens a PowerPoint PPTX file and extracts text from all slides. It also provides the total number of slides in the presentation. ```go package main import ( "fmt" "github.com/young2j/oxmltotext/pptxtotext" ) func main() { pp, err := pptxtotext.Open("presentation.pptx") if err != nil { panic(err) } defer pp.Close() // Get number of slides fmt.Printf("Total slides: %d\n", pp.NumSlides()) // Extract all text texts, err := pp.ExtractTexts() if err != nil { panic(err) } fmt.Println(texts) } ``` -------------------------------- ### Extract PPT using Tika Server Source: https://context7.com/young2j/oxmltotext/llms.txt Extracts text from legacy PPT files using Apache Tika server. This is the only supported method for PPT files. Requires Tika server running on port 9998. ```go package main import ( "fmt" "github.com/young2j/oxmltotext/ppttotext" ) func main() { // Requires Tika server running on port 9998 tikaURL := "http://localhost:9998/tika" // From file path texts, statusCode, err := ppttotext.ExtractFromPathByTika("presentation.ppt", tikaURL) if err != nil { fmt.Printf("Tika response status: %d\n", statusCode) panic(err) } fmt.Println(texts) // From URL texts, statusCode, err = ppttotext.ExtractFromURLByTika( "https://example.com/presentation.ppt", tikaURL, ) if err != nil { panic(err) } fmt.Println(texts) } ``` -------------------------------- ### Extract text from PPT Source: https://github.com/young2j/oxmltotext/blob/master/README.md Extracts text from a .ppt file by communicating with a running Tika server. ```go import ( "fmt" "github.com/young2j/oxmltotext/ppttotext" ) func main() { texts, statusCode, err := ppttotext.ExtractFromPathByTika("../filesamples/file-sample_500kb.ppt", "http://localhost:9998/tika") if err != nil { panic(err) } fmt.Printf("tika server respose status code:%d\n", statusCode) fmt.Println(texts) } ``` -------------------------------- ### Open XLSX File and Extract Text Source: https://context7.com/young2j/oxmltotext/llms.txt Opens an XLSX file and extracts text from all sheets, including cell values and shared strings. It also provides the total number of sheets. ```go package main import ( "fmt" "github.com/young2j/oxmltotext/xlsxtotext" ) func main() { xp, err := xlsxtotext.Open("spreadsheet.xlsx") if err != nil { panic(err) } defer xp.Close() // Get number of sheets fmt.Printf("Total sheets: %d\n", xp.NumSheets()) // Extract all text texts, err := xp.ExtractTexts() if err != nil { panic(err) } fmt.Println(texts) } ``` -------------------------------- ### Extract DOCX with Charts and Diagrams Source: https://context7.com/young2j/oxmltotext/llms.txt Enables text extraction from embedded charts and diagrams within a DOCX file. The SetDrawingsNoFmt option can be used to remove formatting borders from the output. ```go package main import ( "fmt" "github.com/young2j/oxmltotext/docxtotext" ) func main() { dp, err := docxtotext.Open("document.docx") if err != nil { panic(err) } defer dp.Close() // Enable chart and diagram parsing dp.SetParseCharts(true) dp.SetParseDiagrams(true) // Optional: remove formatting borders from output dp.SetDrawingsNoFmt(true) texts, err := dp.ExtractTexts() if err != nil { panic(err) } fmt.Println(texts) // Output includes chart data: // ┌───────────────chart───────────────┐ // [Series 1] // Category 1 Category 2 Category 3 // 4.3 2.5 3.5 // └───────────────────────────────────┘ } ``` -------------------------------- ### Extract XLS using xlstotext Source: https://context7.com/young2j/oxmltotext/llms.txt Extracts text from legacy XLS files using the xlstotext Rust utility. Requires the xlstotext binary to be compiled and added to the system's PATH. ```go package main import ( "fmt" "github.com/young2j/oxmltotext/xlstotext" ) func main() { // Requires xlstotext binary compiled from xlstotext/rs: // cd xlstotext/rs && cargo build --release // Add target/release/xlstotext to PATH // From file path texts, err := xlstotext.ExtractFromPath("spreadsheet.xls") if err != nil { panic(err) } fmt.Println(texts) // From URL texts, statusCode, err := xlstotext.ExtractFromURL("https://example.com/spreadsheet.xls") if err != nil { fmt.Printf("HTTP Status: %d\n", statusCode) panic(err) } fmt.Println(texts) } ``` -------------------------------- ### Extract text from XLS Source: https://github.com/young2j/oxmltotext/blob/master/README.md Extracts text from an .xls file using the xlstotext package. ```go import ( "fmt" "github.com/young2j/oxmltotext/xlstotext" ) func main() { texts, err := xlstotext.ExtractFromPath("../filesamples/file-sample_100kb.xls") if err != nil { panic(err) } fmt.Println(texts) } ``` -------------------------------- ### Extract text from DOC Source: https://github.com/young2j/oxmltotext/blob/master/README.md Extracts text from a .doc file using the doctotext package. ```go import ( "fmt" "github.com/young2j/oxmltotext/doctotext" ) func main() { texts, err := doctotext.ExtractFromPath("../filesamples/file-sample_100kb.doc") if err != nil { panic(err) } fmt.Println(texts) } ``` -------------------------------- ### Extract XLS using Tika Server Source: https://context7.com/young2j/oxmltotext/llms.txt Extracts text from XLS files using Apache Tika server. Ensure the Tika server is running and accessible at the specified URL. ```go package main import ( "fmt" "github.com/young2j/oxmltotext/xlstotext" ) func main() { tikaURL := "http://localhost:9998/tika" texts, statusCode, err := xlstotext.ExtractFromPathByTika("spreadsheet.xls", tikaURL) if err != nil { fmt.Printf("Tika response status: %d\n", statusCode) panic(err) } fmt.Println(texts) } ``` -------------------------------- ### Extract Specific PDF Pages Source: https://context7.com/young2j/oxmltotext/llms.txt Extracts text from specified page numbers (0-indexed) of a PDF file. This is useful when only certain pages need to be processed. ```go package main import ( "fmt" "github.com/young2j/oxmltotext/pdftotext" ) func main() { pp, err := pdftotext.Open("document.pdf") if err != nil { panic(err) } defer pp.Close() // Extract pages 0, 1, and 2 (first three pages) texts, err := pp.ExtractPageTexts(0, 1, 2) if err != nil { panic(err) } fmt.Println(texts) } ``` -------------------------------- ### Extract Images from DOCX Source: https://context7.com/young2j/oxmltotext/llms.txt Extracts raw image data from a DOCX file. Each image is provided with its name and format, and can be saved to a file. ```go package main import ( "fmt" "image/png" "os" "github.com/young2j/oxmltotext/docxtotext" ) func main() { dp, err := docxtotext.Open("document.docx") if err != nil { panic(err) } defer dp.Close() // Extract all images images, err := dp.ExtractImages() if err != nil { panic(err) } for _, img := range images { fmt.Printf("Image: %s, Format: %s\n", img.Name, img.Format) // Save image to file f, err := os.Create(img.Name) if err != nil { continue } png.Encode(f, img.Raw) f.Close() } } ``` -------------------------------- ### Extract Specific PPTX Slides Source: https://context7.com/young2j/oxmltotext/llms.txt Extracts text from specified slide numbers only (1-indexed) in a PPTX file. This allows for targeted text extraction from particular slides. ```go package main import ( "fmt" "github.com/young2j/oxmltotext/pptxtotext" ) func main() { pp, err := pptxtotext.Open("presentation.pptx") if err != nil { panic(err) } defer pp.Close() // Extract only slides 1, 2, and 5 texts, err := pp.ExtractSlideTexts(1, 2, 5) if err != nil { panic(err) } fmt.Println(texts) } ``` -------------------------------- ### Extract text from PDF Source: https://github.com/young2j/oxmltotext/blob/master/README.md Extracts text content from a PDF file using the pdftotext package. ```go import ( "fmt" "github.com/young2j/oxmltotext/pdftotext" ) func main() { pp, err := pdftotext.Open("../filesamples/file-sample_500kb.pdf") if err != nil { panic(err) } defer pp.Close() // Please remember to call the `Close` method to avoid memory leaks. // Extract the text of page 1,2 // texts, err := pp.ExtractPageTexts(1,2) texts, err := pp.ExtractTexts() if err != nil { panic(err) } fmt.Println(texts) } ``` -------------------------------- ### Extract Specific XLSX Sheets Source: https://context7.com/young2j/oxmltotext/llms.txt Extracts text from specified sheet numbers only (1-indexed) in an XLSX file. This is useful for targeting specific data within a spreadsheet. ```go package main import ( "fmt" "github.com/young2j/oxmltotext/xlsxtotext" ) func main() { xp, err := xlsxtotext.Open("spreadsheet.xlsx") if err != nil { panic(err) } defer xp.Close() // Extract only sheets 1 and 3 texts, err := xp.ExtractSheetTexts(1, 3) if err != nil { panic(err) } fmt.Println(texts) } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.