Documentation
¶
Overview ¶
Package gofastmsbin extracts the binary Office formats into the shared go-fast document model, gofastcore.Document: Word documents (.doc), Excel workbooks (.xls) and PowerPoint presentations (.ppt) as Office 97 to 2003 wrote them, with their templates and shows, and the workbooks of Excel 5 and 95 and the documents of Word 6 and 95. Their gofastcore.Source.Format is doc, xls and ppt.
Extract and ExtractFile read a whole file into a Document: a body part for a Word document, a sheet part for each sheet of a workbook with its cells as tables, a slide part for each slide of a presentation. Open gives a File, from which the parts are read one at a time (Parts, Part) or as a Document; a workbook's sheets and a presentation's slides are read in parallel, and the result does not depend on how many at a time.
The properties of the file are its Metadata. The source of its VBA modules, its defined names and the like are in a part of kind stored, the last. The files embedded in it are its Children, read by the extractor given as Options.Child.
An encrypted file is opened with Options.Password.
What the file declares of the display of its content (a hidden sheet, row, slide or shape; hidden text) is reported on the block or the part, and nothing is judged: content is given whether or not it is shown.
The format is decided from the file, never from its name: all three are compound files, told apart by the streams they hold. A file of an older generation (Excel 2 to 4, Word 2, PowerPoint 95) or of another format is refused with an error that says what it is.
The library has no dependencies beyond the standard library and go-fast-core. The gofastmsbin command also uses go-fast-ooxml, to read the Office Open XML files embedded in a document.
Index ¶
- Constants
- func Detect(r io.ReaderAt, size int64) string
- func Extract(ctx context.Context, r io.ReaderAt, size int64, opts *gofastcore.Options) (*gofastcore.Document, error)
- func ExtractFile(ctx context.Context, path string, opts *gofastcore.Options) (*gofastcore.Document, error)
- type Extractor
- func (e *Extractor) Extract(ctx context.Context, r io.ReaderAt, size int64, opts *gofastcore.Options) (*gofastcore.Document, error)
- func (e *Extractor) ExtractFile(ctx context.Context, path string, opts *gofastcore.Options) (*gofastcore.Document, error)
- func (e *Extractor) Formats() []string
- func (e *Extractor) Open(path string, opts *gofastcore.Options) (*File, error)
- func (e *Extractor) OpenBytes(data []byte, opts *gofastcore.Options) (*File, error)
- func (e *Extractor) OpenReaderAt(r io.ReaderAt, size int64, opts *gofastcore.Options) (*File, error)
- type File
- func (f *File) Children(ctx context.Context) ([]*gofastcore.Document, error)
- func (f *File) Close() error
- func (f *File) Document(ctx context.Context) (*gofastcore.Document, error)
- func (f *File) Format() string
- func (f *File) Metadata() gofastcore.Metadata
- func (f *File) NumParts() int
- func (f *File) Part(ctx context.Context, number int) (gofastcore.Part, error)
- func (f *File) Parts(ctx context.Context) iter.Seq2[gofastcore.Part, error]
- func (f *File) Sheets() []Sheet
- func (f *File) Source() gofastcore.Source
- func (f *File) Warnings() []gofastcore.Warning
- type Sheet
Examples ¶
Constants ¶
const ( // DefaultMaxInputBytes bounds the size of the file. DefaultMaxInputBytes = 1 << 30 // DefaultMaxDecodedBytes bounds the bytes the file is expanded to // beyond its own size, over the document and the files embedded in // it: macro code and pictures the file keeps compressed, and embedded // files written out to be read. DefaultMaxDecodedBytes = 2 << 30 // DefaultMaxCells bounds the cells kept from a workbook, over all its // sheets. Each cell kept costs memory in the result whatever the size // of the file, so this is what bounds the memory a workbook can ask // for. DefaultMaxCells = 20_000_000 )
Defaults for the limits of gofastcore.Options.Limits, used where a limit is zero. A negative limit means no limit.
Variables ¶
This section is empty.
Functions ¶
func Detect ¶ added in v0.1.3
Detect reports which of the three formats r is, as Extractor.Formats names them: "doc", "xls" or "ppt". It gives "" for input that is not a compound file, for a compound file that is none of the three (an Outlook message, an object another program stored), and for one whose directory cannot be read.
It decides as Extract does, from the names of the streams in the compound file's root storage, and reads the file's header and directory only, never the content of a stream. So it names the format of every compound file Extract takes for one of the three, among them those Extract then refuses: a generation that is not read, an encryption that is not read, a file damaged outside its directory. A file of an old generation that is no compound file (Excel 2 to 4, Word 2) gives "".
It is for a program that reads several kinds of compound file and has to choose a library for one.
func Extract ¶
func Extract(ctx context.Context, r io.ReaderAt, size int64, opts *gofastcore.Options) (*gofastcore.Document, error)
Extract reads a file with default settings. It is (&Extractor{}).Extract.
Example ¶
A whole file as one document.
data := msbintest.BookFile(msbintest.Workbook(nil, msbintest.Sheet{Name: "Prices", Records: [][]byte{
msbintest.Label(0, 0, 0, "Item"), msbintest.Label(0, 1, 0, "Price"),
msbintest.Label(1, 0, 0, "Tea"), msbintest.Number(1, 1, 0, 2.5),
}}))
f, err := gofastmsbin.OpenBytes(data, nil)
if err != nil {
fmt.Println(err)
return
}
defer f.Close()
doc, err := f.Document(context.Background())
if err != nil {
fmt.Println(err)
return
}
fmt.Println(doc.Source.Format, len(doc.Parts), doc.Parts[0].Name)
for _, row := range doc.Parts[0].Blocks[0].Table.Rows {
fmt.Println(row)
}
Output: xls 1 Prices [Item Price] [Tea 2.5]
Example (Password) ¶
An encrypted file is opened with its password.
stream := msbintest.Workbook(nil, msbintest.Sheet{Name: "Private", Records: [][]byte{msbintest.Label(0, 0, 0, "for your eyes only")}})
data := msbintest.BookFile(msbintest.EncryptWorkbook(stream, msbintest.CryptRC4, "sesame"))
_, err := gofastmsbin.Extract(context.Background(), bytes.NewReader(data), int64(len(data)), nil)
fmt.Println(err)
doc, err := gofastmsbin.Extract(context.Background(), bytes.NewReader(data), int64(len(data)), &gofastcore.Options{Password: "sesame"})
if err != nil {
fmt.Println(err)
return
}
fmt.Println(doc.Parts[0].Blocks[0].Table.Rows[0][0])
fmt.Println(doc.Warnings[0].Code)
Output: gofastmsbin: Excel 97-2003 (BIFF8) file, encrypted (RC4): no password given: gofastcore: password required for your eyes only input.decrypted
Types ¶
type Extractor ¶
type Extractor struct {
// MaxCells bounds the cells kept from a workbook, over all its
// sheets. A sheet that reaches it is cut off there, with a
// limit.exceeded warning. Zero means DefaultMaxCells; negative means
// no limit.
MaxCells int64
}
Extractor extracts Word, Excel and PowerPoint files in the binary formats of Office 97 to 2003. The zero value is ready to use and safe for concurrent use. Its fields are the settings only this library has; everything else is in gofastcore.Options.
func (*Extractor) Extract ¶
func (e *Extractor) Extract(ctx context.Context, r io.ReaderAt, size int64, opts *gofastcore.Options) (*gofastcore.Document, error)
Extract reads size bytes from r and returns the document.
The format is decided from the file itself, never from a name: a Word document, a workbook or a presentation of the binary formats.
An encrypted file is opened with Options.Password; a workbook encrypted with the password Excel uses when none was set is opened without one. The document then has Metadata.Encrypted set and the warning input.decrypted.
An error wraps one of the gofastcore errors:
- ErrLimitExceeded: the input is larger than Limits.MaxInputBytes, or its compound file has more entries than are read.
- ErrMalformed: a compound file too damaged to say what it holds.
- ErrUnsupported: a file that is none of the three formats; one of a generation that is not read (Excel 2 to 4, Word 2, PowerPoint 95 and older); a file encrypted in a way that is not read.
- ErrPasswordRequired, ErrWrongPassword: the file is encrypted, and no password was given, or the one given does not open it.
- ErrNoPartsSelected: Options.Parts selects none of the document's parts.
It is OpenReaderAt followed by Document.
func (*Extractor) ExtractFile ¶
func (e *Extractor) ExtractFile(ctx context.Context, path string, opts *gofastcore.Options) (*gofastcore.Document, error)
ExtractFile opens the named file and extracts it. The file's base name is recorded as the source name unless opts.Name is set.
func (*Extractor) Formats ¶
Formats lists the formats this library extracts, as they appear in gofastcore.Source.Format.
func (*Extractor) Open ¶
Open opens the named file. The file's base name is recorded as the source name unless opts.Name is set. Close the File when done with it.
type File ¶
type File struct {
// contains filtered or unexported fields
}
File is a Word, Excel or PowerPoint file opened for reading a part at a time: a Word document's one body, a workbook's sheets, a presentation's slides. Opening a file identifies it and reads its properties and what its parts share; the parts are read when they are asked for, so a caller that handles one part and lets it go holds little at a time however large the file is.
The parts are the same as those in the document that Extract returns, each complete: its warnings are on it, and what the file declares about the display of its text is set (Part.Finish has been called).
A File is safe for concurrent use. A limit of Options.Limits counts over one pass: one call of Document, Part or Children, or one run through Parts. Passes that run at the same time share it.
func OpenReaderAt ¶
OpenReaderAt opens a file of the given size read through r, with default settings.
func (*File) Children ¶
Children extracts the files embedded in the document (a workbook in a Word document, a document in a document) through Options.Child, and returns them as in gofastcore.Document.Children. With no Options.Child each is listed, not extracted. They have what the parts read last left of the text the document may hold (Limits.MaxTextBytes), as in Document.
func (*File) Document ¶
Document reads the parts Options.Parts selects and returns the document, the same one Extract returns.
func (*File) Format ¶
Format returns the kind of file: "doc", "xls" or "ppt". A template is the format it is a template of.
func (*File) Metadata ¶
func (f *File) Metadata() gofastcore.Metadata
Metadata returns the document's properties.
func (*File) NumParts ¶
NumParts returns the number of parts: 1 for a Word document, the number of sheets of a workbook, the number of slides of a presentation. A workbook or presentation with none has one part, which says so. A document that stores what it shows nowhere (defined names, macro code) has one more, last, of kind gofastcore.PartStored; it comes with the whole document, not with a range of parts.
func (*File) Part ¶
Part reads the part with the given number, from 1, whether or not Options.Parts selects it.
func (*File) Parts ¶
Parts returns an iterator over the parts Options.Parts selects, in order. Each part is read when the loop reaches it, a few ahead with more than one worker, and is the caller's to keep or let go. The bytes of a picture that several parts show (Options.Images) come with the first of them.
The iterator yields an error, with an empty part, and stops: when no part is selected (gofastcore.ErrNoPartsSelected), when ctx is cancelled, or when the file can no longer be read. A part that is damaged or cut short by a limit is not an error: it is yielded with a warning that says so.
What the parts say about the document as a whole is in Warnings and Children once they have been read.
Example ¶
A part at a time: the sheets of a workbook or the slides of a presentation are read as they are asked for, and nothing is kept of the ones that were read.
var sheets []msbintest.Sheet
for _, name := range []string{"January", "February", "March"} {
sheets = append(sheets, msbintest.Sheet{Name: name, Records: [][]byte{msbintest.Label(0, 0, 0, "Sales in "+name)}})
}
f, err := gofastmsbin.OpenBytes(msbintest.BookFile(msbintest.Workbook(nil, sheets...)), &gofastcore.Options{Workers: 1})
if err != nil {
fmt.Println(err)
return
}
defer f.Close()
for _, sheet := range f.Sheets() {
fmt.Println(sheet.Number, sheet.Name)
}
for part, err := range f.Parts(context.Background()) {
if err != nil {
fmt.Println(err)
return
}
fmt.Println(part.Number, part.Blocks[0].Table.Rows[0][0])
}
Output: 1 January 2 February 3 March 1 Sales in January 2 Sales in February 3 Sales in March
func (*File) Sheets ¶
Sheets lists the sheets of a workbook in order, without reading them. It is empty for other formats.
func (*File) Source ¶
func (f *File) Source() gofastcore.Source
Source returns what is known of where the document came from, as in gofastcore.Document.Source.
func (*File) Warnings ¶
func (f *File) Warnings() []gofastcore.Warning
Warnings returns the warnings about the document as a whole, as in gofastcore.Document.Warnings: those known when the file was opened, and those that the parts read so far give rise to (content of a kind that is not read, damaged supporting parts, references to things outside the file). It is complete once every part has been read.
Directories
¶
| Path | Synopsis |
|---|---|
|
cmd
|
|
|
gofastmsbin
command
Command gofastmsbin extracts Word, Excel and PowerPoint files in the binary formats of Office 97 to 2003 (.doc, .xls, .ppt) into the shared document model of the go-fast libraries and prints them as JSON, Markdown or plain text.
|
Command gofastmsbin extracts Word, Excel and PowerPoint files in the binary formats of Office 97 to 2003 (.doc, .xls, .ppt) into the shared document model of the go-fast libraries and prints them as JSON, Markdown or plain text. |
|
internal
|
|
|
cfb
Package cfb reads compound files, the container of the legacy Office formats, and writes them.
|
Package cfb reads compound files, the container of the legacy Office formats, and writes them. |
|
chart
Package chart turns the data of a chart into blocks, as go-fast-ooxml does: the names of its series, its categories and its values, given as a table, with the chart's title as a caption before it and its type and the titles of its axes as attributes (gofastcore.RoleChart).
|
Package chart turns the data of a chart into blocks, as go-fast-ooxml does: the names of its series, its categories and its values, given as a table, with the chart's title as a caption before it and its type and the titles of its axes as attributes (gofastcore.RoleChart). |
|
clean
Package clean turns bytes read from a file into strings that obey the text conventions of the shared document model: valid UTF-8, no control characters other than line feed and tab, and for prose no surrounding white space and no presentation-form ligatures.
|
Package clean turns bytes read from a file into strings that obey the text conventions of the shared document model: valid UTF-8, no control characters other than line feed and tab, and for prose no surrounding white space and no presentation-form ligatures. |
|
codepage
Package codepage turns bytes in a code page into Unicode.
|
Package codepage turns bytes in a code page into Unicode. |
|
crypt
Package crypt opens the encryption of the binary Office formats ([MS-OFFCRYPTO] 2.3): XOR obfuscation, the RC4 encryption of Office 97, and RC4 with a key made by the CryptoAPI.
|
Package crypt opens the encryption of the binary Office formats ([MS-OFFCRYPTO] 2.3): XOR obfuscation, the RC4 encryption of Office 97, and RC4 with a key made by the CryptoAPI. |
|
detect
Package detect says what a file is: which of the three formats, written by which generation of the program, and whether it is encrypted.
|
Package detect says what a file is: which of the three formats, written by which generation of the program, and whether it is encrypted. |
|
doc
Package doc reads Word documents in the binary format of Word 97 to 2003 ([MS-DOC]): the WordDocument stream, which holds the text and the pages of properties, and the table stream beside it, which holds everything that says what the text is.
|
Package doc reads Word documents in the binary format of Word 97 to 2003 ([MS-DOC]): the WordDocument stream, which holds the text and the pages of properties, and the table stream beside it, which holds everything that says what the text is. |
|
embed
Package embed finds the objects a file of the three formats embeds as storages of its compound file, and hands each on as a file of its own.
|
Package embed finds the objects a file of the three formats embeds as storages of its compound file, and hands each on as a file of its own. |
|
grid
Package grid turns the cells of a sheet into table blocks.
|
Package grid turns the cells of a sheet into table blocks. |
|
hidden
Package hidden places what the file declares about the display of text (hidden, no ink) on blocks and spans, as go-fast-ooxml's readers share it.
|
Package hidden places what the file declares about the display of text (hidden, no ink) on blocks and spans, as go-fast-ooxml's readers share it. |
|
label
Package label writes the labels a reader sees in front of list items: bullets, and numbers in the forms Word and PowerPoint both use.
|
Package label writes the labels a reader sees in front of list items: bullets, and numbers in the forms Word and PowerPoint both use. |
|
media
Package media makes the image blocks of pictures the three formats keep through the drawing layer.
|
Package media makes the image blocks of pictures the three formats keep through the drawing layer. |
|
msbintest
Package msbintest is the test harness of go-fast-msbin: it builds the files the library reads, so that each behaviour has a small file with exactly known content.
|
Package msbintest is the test harness of go-fast-msbin: it builds the files the library reads, so that each behaviour has a small file with exactly known content. |
|
mtef
Package mtef reads the equations of the Equation Editor and of MathType, which the binary Office formats keep as embedded objects: a stream "Equation Native" that holds the equation in MTEF, a sequence of records for lines, characters, templates (a fraction, a root, a sum), piles and matrices.
|
Package mtef reads the equations of the Equation Editor and of MathType, which the binary Office formats keep as embedded objects: a stream "Equation Native" that holds the equation in MTEF, a sequence of records for lines, characters, templates (a fraction, a root, a sum), piles and matrices. |
|
officeart
Package officeart reads the drawing layer that Word, Excel and PowerPoint files of the binary formats share ([MS-ODRAW]): shapes with their properties and where they are anchored, and the pictures kept for them.
|
Package officeart reads the drawing layer that Word, Excel and PowerPoint files of the binary formats share ([MS-ODRAW]): shapes with their properties and where they are anchored, and the pictures kept for them. |
|
oleps
Package oleps reads property sets ([MS-OLEPS]): the streams "\x05SummaryInformation" and "\x05DocumentSummaryInformation", in which a Word, Excel or PowerPoint file keeps its title, its author, its dates and the properties its author defined.
|
Package oleps reads property sets ([MS-OLEPS]): the streams "\x05SummaryInformation" and "\x05DocumentSummaryInformation", in which a Word, Excel or PowerPoint file keeps its title, its author, its dates and the properties its author defined. |
|
ppt
Package ppt reads PowerPoint presentations in the binary format of PowerPoint 97 to 2003 ([MS-PPT]): the PowerPoint Document stream, a tree of records, of which the presentation as it now stands is found through the edits the Current User stream leads to.
|
Package ppt reads PowerPoint presentations in the binary format of PowerPoint 97 to 2003 ([MS-PPT]): the PowerPoint Document stream, a tree of records, of which the presentation as it now stands is found through the edits the Current User stream leads to. |
|
stored
Package stored reads what a file of any of the three formats keeps for the file as a whole and shows on no page: the code of its VBA projects.
|
Package stored reads what a file of any of the three formats keeps for the file as a whole and shows on no page: the code of its VBA projects. |
|
symbol
Package symbol translates the characters of symbol fonts to Unicode.
|
Package symbol translates the characters of symbol fonts to Unicode. |
|
unit
Package unit is what the readers of the three formats have in common when a document is read a part at a time: a Word document's one body, a workbook's sheets, a presentation's slides.
|
Package unit is what the readers of the three formats have in common when a document is read a part at a time: a Word document's one body, a workbook's sheets, a presentation's slides. |
|
vba
Package vba reads the source code of a VBA project: the modules of macros an Office file carries in a storage of its compound file (a presentation, in a compound file of its own within its stream).
|
Package vba reads the source code of a VBA project: the modules of macros an Office file carries in a storage of its compound file (a presentation, in a compound file of its own within its stream). |
|
warn
Package warn holds what the readers share in saying what a fixed bound left out, and the messages of warnings several of them raise.
|
Package warn holds what the readers share in saying what a fixed bound left out, and the messages of warnings several of them raise. |
|
xls
Package xls reads Excel workbooks in the binary format of Excel 97 to 2003 (BIFF8, [MS-XLS]) and of Excel 5 and 95 (BIFF5): the Workbook stream, or the Book stream of the older format, a run of records.
|
Package xls reads Excel workbooks in the binary format of Excel 97 to 2003 (BIFF8, [MS-XLS]) and of Excel 5 and 95 (BIFF5): the Workbook stream, or the Book stream of the older format, a run of records. |