Create a package
Open XML files are ZIP-based packages. A valid package needs content types, relationships, and at least the required root part for the document category you are creating.
In ooxmlsdk, package read/write APIs are available through types such as WordprocessingDocument, SpreadsheetDocument, and PresentationDocument. Use each document type's create(...) constructor to start a new package, add the required main part and child parts, then save to the file or writer your application owns.
Rust workflow
The recommended documented workflow is:
- Pick the package family and document type.
- Create the package with
WordprocessingDocument::create,SpreadsheetDocument::create, orPresentationDocument::create. - Add the required main part and any child parts.
- Set part bytes or generated root elements.
- Save the package to a writer or file.
The domain-specific create chapters show minimal package writers:
- Create a word processing document by providing a file name
- Create a spreadsheet document by providing a file name
- Create a presentation document by providing a file name
#![allow(unused)] fn main() { use std::io::Cursor; use std::path::Path; use ooxmlsdk::parts::theme_part::ThemePart; use ooxmlsdk::parts::wordprocessing_document::WordprocessingDocument; use ooxmlsdk::sdk::{OpenSettings, PackageOpenMode, is_encrypted_office_file_path}; pub fn round_trip_word_document(path: &Path) -> Result<Vec<u8>, Box<dyn std::error::Error>> { let document = WordprocessingDocument::new_from_file(path)?; let main_part = document.main_document_part().expect("main document part"); assert!(document.get_id_of_part(&main_part).is_some()); let mut buffer = Cursor::new(Vec::new()); document.save(&mut buffer)?; Ok(buffer.into_inner()) } }
Document type and extension
The document type controls the content type written for the main part. Keep it aligned with the extension you persist:
| Family | Normal | Template | Macro-enabled |
|---|---|---|---|
| WordprocessingML | .docx with WordprocessingDocumentType::Document | .dotx with Template | .docm / .dotm with macro-enabled variants |
| SpreadsheetML | .xlsx with SpreadsheetDocumentType::Workbook | .xltx with Template | .xlsm, .xltm, or .xlam with macro-enabled variants |
| PresentationML | .pptx with PresentationDocumentType::Presentation | .potx with Template | .pptm, .potm, .ppsm, or .ppam with macro-enabled variants |
Office applications can reject a package whose extension does not match its main part content type.
Templates
Use create_from_template when a .dotx, .xltx, or .potx should become an editable regular document package. The method opens the template, changes the package document type to the default regular type for that family, and preserves the package content for further mutation and saving.
WordprocessingML structure
The minimum main document part for a word-processing package is a w:document root element with a w:body child:
<w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
<w:body/>
</w:document>
The corresponding generated Rust types live under:
ooxmlsdk::schemas::schemas_openxmlformats_org_wordprocessingml_2006_main
For more about WordprocessingML package structure, see Structure of a WordprocessingML document.