Skip to content

Конвертація Office

Конвертація документів Microsoft Office (Word, Excel, PowerPoint) у PDF без встановленого Microsoft Office чи LibreOffice. PDF Oxide безпосередньо парсить формат OOXML та генерує PDF-вивід.

Швидкий приклад

Python

from pdf_oxide import OfficeConverter

# Auto-detect format from extension
pdf = OfficeConverter.convert("report.docx")
pdf.save("report.pdf")

Rust

use pdf_oxide::converters::office::OfficeConverter;

let converter = OfficeConverter::new();
let pdf_bytes = converter.convert("report.docx")?;
std::fs::write("report.pdf", pdf_bytes)?;

Підтримувані формати

Format Extension Description
DOCX .docx Word documents — paragraphs, headings, lists, text formatting
XLSX .xlsx, .xls Excel spreadsheets — multi-sheet, auto-sized columns, cell types
PPTX .pptx PowerPoint presentations — slides, titles, text boxes

Word Documents (DOCX)

Конвертувати документи Word зі збереженням заголовків, абзаців, списків та форматування тексту (жирний, курсив, підкреслення, кольори, розміри шрифтів).

Python

from pdf_oxide import OfficeConverter

pdf = OfficeConverter.from_docx("document.docx")
pdf.save("document.pdf")

Rust

use pdf_oxide::converters::office::OfficeConverter;

let converter = OfficeConverter::new();
let pdf_bytes = converter.convert_docx("document.docx")?;
std::fs::write("document.pdf", pdf_bytes)?;

From Bytes

Python

from pdf_oxide import OfficeConverter

with open("document.docx", "rb") as f:
    pdf = OfficeConverter.from_docx_bytes(f.read())
pdf.save("document.pdf")

Rust

let docx_bytes = std::fs::read("document.docx")?;
let converter = OfficeConverter::new();
let pdf_bytes = converter.convert_docx_bytes(&docx_bytes)?;
std::fs::write("document.pdf", pdf_bytes)?;

DOCX Features Supported

  • Paragraphs with alignment (left, center, right, justified)
  • Headings (Heading 1–9 styles)
  • Text formatting: bold, italic, underline, strikethrough
  • Розмір шрифтуs and colors
  • Numbered and bulleted lists with nesting
  • Metadata extraction (title, author from docProps/core.xml)

Excel Spreadsheets (XLSX)

Конвертувати електронні таблиці у PDF з автоматично розрахованими ширинами стовпців та підтримкою кількох аркушів. Кожен аркуш рендериться як окремий розділ.

Python

from pdf_oxide import OfficeConverter

pdf = OfficeConverter.from_xlsx("data.xlsx")
pdf.save("data.pdf")

Rust

let converter = OfficeConverter::new();
let pdf_bytes = converter.convert_xlsx("data.xlsx")?;
std::fs::write("data.pdf", pdf_bytes)?;

XLSX Features Supported

  • Multi-sheet rendering with sheet titles
  • Cell types: strings, integers, floats, booleans, dates, errors
  • Automatic column width calculation
  • Automatic page breaks when content exceeds available space

PowerPoint Presentations (PPTX)

Конвертувати презентації у PDF. Кожен слайд стає сторінкою з вилученими заголовками та текстовими полями.

Python

from pdf_oxide import OfficeConverter

pdf = OfficeConverter.from_pptx("slides.pptx")
pdf.save("slides.pdf")

Rust

let converter = OfficeConverter::new();
let pdf_bytes = converter.convert_pptx("slides.pptx")?;
std::fs::write("slides.pdf", pdf_bytes)?;

Configuration (Rust)

Customize page size, margins, and fonts using OfficeConfig:

use pdf_oxide::converters::office::{OfficeConverter, OfficeConfig};

let config = OfficeConfig::a4(); // A4 page size
let converter = OfficeConverter::with_config(config);
let pdf_bytes = converter.convert_docx("document.docx")?;

OfficeConfig Fields

Поле Тип За замовчуванням Опис
page_size PageSize Letter Page dimensions
margins Margins 1 inch all sides Page margins in points (72pt = 1 inch)
embed_fonts bool false Whether to embed fonts
default_font String "Helvetica" Fallback font
default_font_size f32 11.0 Default text size in points
line_height f32 1.2 Line height multiplier
include_images bool true Include embedded images

Сторінка Size Presets

let config = OfficeConfig::letter(); // 8.5 × 11 inches (default)
let config = OfficeConfig::a4();     // 210 × 297 mm

Custom Margins

use pdf_oxide::converters::office::Margins;

let mut config = OfficeConfig::letter();
config.margins = Margins::uniform(36.0);  // 0.5 inch margins
config.margins = Margins::none();          // No margins

Batch Conversion

Python

from pdf_oxide import OfficeConverter
from pathlib import Path

office_dir = Path("documents/")
output_dir = Path("pdfs/")
output_dir.mkdir(exist_ok=True)

extensions = {".docx", ".xlsx", ".pptx"}

for doc_path in office_dir.iterdir():
    if doc_path.suffix.lower() in extensions:
        pdf = OfficeConverter.convert(str(doc_path))
        pdf.save(str(output_dir / doc_path.with_suffix(".pdf").name))
        print(f"Converted: {doc_path.name}")

Rust

use pdf_oxide::converters::office::OfficeConverter;
use std::fs;

let converter = OfficeConverter::new();

for entry in fs::read_dir("documents/")? {
    let path = entry?.path();
    match path.extension().and_then(|e| e.to_str()) {
        Some("docx" | "xlsx" | "pptx") => {
            let pdf_bytes = converter.convert(&path)?;
            let out = format!("pdfs/{}.pdf", path.file_stem().unwrap().to_str().unwrap());
            fs::write(&out, pdf_bytes)?;
            println!("Converted: {}", path.display());
        }
        _ => {}
    }
}

Довідник API

Python — OfficeConverter

Метод Повертає Опис
OfficeConverter.convert(path) Pdf Auto-detect format and convert
OfficeConverter.from_docx(path) Pdf Convert DOCX file
OfficeConverter.from_docx_bytes(data) Pdf Convert DOCX from bytes
OfficeConverter.from_xlsx(path) Pdf Convert XLSX file
OfficeConverter.from_xlsx_bytes(data) Pdf Convert XLSX from bytes
OfficeConverter.from_pptx(path) Pdf Convert PPTX file
OfficeConverter.from_pptx_bytes(data) Pdf Convert PPTX from bytes

All methods return a Pdf object. Call pdf.save("output.pdf") or pdf.to_bytes() to get the result.

Rust — OfficeConverter

Метод Повертає Опис
OfficeConverter::new() OfficeConverter Create with default config
OfficeConverter::with_config(config) OfficeConverter Create with custom config
convert(path) Result<Vec<u8>> Auto-detect format and convert
convert_docx(path) Result<Vec<u8>> Convert DOCX file
convert_docx_bytes(bytes) Result<Vec<u8>> Convert DOCX from bytes
convert_xlsx(path) Result<Vec<u8>> Convert XLSX file
convert_xlsx_bytes(bytes) Result<Vec<u8>> Convert XLSX from bytes
convert_pptx(path) Result<Vec<u8>> Convert PPTX file
convert_pptx_bytes(bytes) Result<Vec<u8>> Convert PPTX from bytes

Пов’язані сторінки