Skip to main content

Document

Struct Document 

Source
pub struct Document<R> { /* private fields */ }
Expand description

A Word document (.docx file).

This is the main entry point for reading Word documents. The document stores parsed generated types (types::Document, types::Styles) that can be queried using the extension traits in ext.

For writing documents, use DocumentBuilder.

Implementations§

Source§

impl Document<BufReader<File>>

Source

pub fn open<P: AsRef<Path>>(path: P) -> Result<Self>

Open a Word document from a file path.

Source§

impl<R: Read + Seek> Document<R>

Source

pub fn from_reader(reader: R) -> Result<Self>

Open a Word document from a reader.

Source

pub fn body(&self) -> &Body

Get the document body.

Returns the generated Body type. Use extension traits from ext to access paragraphs, runs, and text content.

Source

pub fn body_mut(&mut self) -> &mut Body

Get a mutable reference to the document body.

Source

pub fn gen_doc(&self) -> &Document

Get the generated document.

Source

pub fn package(&self) -> &Package<R>

Get the underlying package.

Source

pub fn package_mut(&mut self) -> &mut Package<R>

Get a mutable reference to the underlying package.

Source

pub fn styles(&self) -> &Styles

Get the document styles (generated types).

Source

pub fn core_properties(&self) -> Option<&CoreProperties>

Get the core document properties (title, author, etc.).

Returns None if the document doesn’t have a core properties part.

Source

pub fn app_properties(&self) -> Option<&AppProperties>

Get the extended application properties (word count, page count, etc.).

Returns None if the document doesn’t have an app properties part.

Source

pub fn text(&self) -> String

Extract all text from the document.

Paragraphs are separated by newlines.

Source

pub fn get_image_data(&mut self, rel_id: &str) -> Result<ImageData>

Get image data by relationship ID.

Looks up the relationship, reads the image file from the package, and returns the image data with its content type.

Get the URL for a hyperlink by its relationship ID.

Returns None if the relationship doesn’t exist.

Source

pub fn doc_relationships(&self) -> &Relationships

Get document relationships (for advanced use).

Source

pub fn get_header(&mut self, rel_id: &str) -> Result<HeaderFooter>

Load a header part by its relationship ID.

Returns the parsed header as a generated HeaderFooter type.

Load a footer part by its relationship ID.

Returns the parsed footer as a generated HeaderFooter type.

Source

pub fn get_footnotes(&mut self) -> Result<Footnotes>

Load the footnotes part.

Returns the parsed footnotes as a generated Footnotes type.

Returns Error::MissingPart if the document has no footnotes.xml.

Source

pub fn get_endnotes(&mut self) -> Result<Endnotes>

Load the endnotes part.

Returns the parsed endnotes as a generated Endnotes type.

Returns Error::MissingPart if the document has no endnotes.xml.

Source

pub fn get_comments(&mut self) -> Result<Comments>

Load the comments part.

Returns the parsed comments as a generated Comments type.

Returns Error::MissingPart if the document has no comments.xml.

Source

pub fn get_settings(&mut self) -> Result<DocumentSettings>

Load the document settings.

Returns the parsed settings from word/settings.xml.

Returns Error::MissingPart if the document has no settings.xml.

Source

pub fn get_chart(&mut self, rel_id: &str) -> Result<ChartSpace>

Load a chart part by its relationship ID.

Looks up the chart relationship in the document’s .rels file, reads the chart XML part (e.g. word/charts/chart1.xml), and parses it as a ChartSpace using the ooxml-dml generated parser.

Chart relationship IDs can be found by walking drawing elements with DrawingChartExt::all_chart_rel_ids().

Requires the wml-charts feature.

ECMA-376 Part 1, §21.2.2.27 (chartSpace).

Source

pub fn save<P: AsRef<Path>>(&mut self, path: P) -> Result<()>

Save the document to a file.

This serializes the current state of the generated types (gen_doc, gen_styles) back into the package, preserving all other parts verbatim.

Source

pub fn write<W: Write + Seek>(&mut self, writer: W) -> Result<()>

Write the document to a writer.

Serializes the document and styles using the generated ToXml serializers, then copies all package parts to the output, replacing only the modified parts.

Auto Trait Implementations§

§

impl<R> Freeze for Document<R>
where R: Freeze,

§

impl<R> RefUnwindSafe for Document<R>
where R: RefUnwindSafe,

§

impl<R> Send for Document<R>
where R: Send,

§

impl<R> Sync for Document<R>
where R: Sync,

§

impl<R> Unpin for Document<R>
where R: Unpin,

§

impl<R> UnsafeUnpin for Document<R>
where R: UnsafeUnpin,

§

impl<R> UnwindSafe for Document<R>
where R: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.