Coder Social home page Coder Social logo

go-read-docx's Introduction

go-read-docx

simple way to read a docx in golang, including support for tables

package main

import (
	docx "github.com/khnom5000/go-read-docx"
)

func main() {
	d, reader, err := docx.GetDocument("./TestDocument.docx")
	if err != nil {
		panic(err)
	}
}

Example - Read text in a docx:

Code:

...
ps := d.Body.Paragraphs
for i, p := range ps {
	fmt.Println("Para:", i, p)
}
...

Example - Read a table thats inside a docx:

Input:

+-----+---+---+----+
|   1 | 2 | 3 |  4 |
|   8 | 8 | 8 | 66 |
| 123 | 1 | 1 |  1 |
|     |   |   |    |
+-----+---+---+----+

Code:

...
t := d.Body.Tables[0].TableRows
var table [][]string
for _, r := range t {
	var row []string
	for _, c := range r.TableColumns {
		row = append(row, c.Cell)
	}
	table = append(table, row)
}
fmt.Println(table)
...

Output:

[[1 2 3 4] [8 8 8 66] [123 1 1 1] [   ]]

Example - Read more than one table in the same docx:

Input:

+-----+-----+-----+-----+
|   1 |   2 |   3 |   4 |
|   8 |   8 |   8 |  66 |
| 123 |   1 |   1 |   1 |
|     |     |     |     |
+-----+-----+-----+-----+
...
+-----+-----+-----+-----+
|   7 |   8 |   9 |   0 |
|   0 |  33 |  66 |  99 |
| 123 | 100 | 100 | 100 |
|     |     |     |     |
+-----+-----+-----+-----+

Code:

...
ts := d.Body.Tables
for _, t := range ts {
	var table [][]string
	for _, tr := range t.TableRows {
		var row []string
		for _, tc := range tr.TableColumns {
			row = append(row, tc.Cell)
		}
		table = append(table, row)
	}
	fmt.Println(table)
}
...

Output:

[[1 2 3 4] [8 8 8 66] [123 1 1 1] [   ]]
[[7 8 9 0] [0 33 66 99] [123 100 100 100] [   ]]

Example - Get the Headers:

...
h, err := docx.GetHeader("./TestDocument.docx")
if err != nil {
	panic(err)
}
fmt.Println(h.Text)
...

The above also works for footers just swap GetHeader() with GetFooter()

Output:

Show all paragraphs
Para: 0 Start of page one.
Para: 1 This is a table.
Para: 2 This is the second table.
Para: 3 This is the end of the doc!
Show first table
[[1 2 3 4] [8 8 8 66] [123 1 1 1] [   ]]
Show all tables
[[1 2 3 4] [8 8 8 66] [123 1 1 1] [   ]]
[[7 8 9 0] [0 33 66 99] [123 100 100 100] [   ]]
Show Header
This is a header.
Show Footer
This is a footer.

Go Reference

go-read-docx's People

Contributors

khnom5000 avatar

Stargazers

yunzheyue avatar Ignacior avatar  avatar

Watchers

 avatar

Forkers

chabad360

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.