将 Office 文档转换为 PDF
将 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)?;
支持的格式
| 格式 | 扩展名 | 描述 |
|---|---|---|
| DOCX | .docx |
Word 文档 — 段落、标题、列表、文本格式 |
| XLSX | .xlsx, .xls |
Excel 电子表格 — 多工作表、自动列宽、单元格类型 |
| PPTX | .pptx |
PowerPoint 演示文稿 — 幻灯片、标题、文本框 |
Word 文档(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)?;
从字节
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 功能
- 带对齐的段落(左对齐、居中、右对齐、两端对齐)
- 标题(标题 1–9 样式)
- 文本格式:粗体、斜体、下划线、删除线
- 字体大小和颜色
- 带嵌套的有序和无序列表
- 元数据提取(从
docProps/core.xml获取标题、作者)
Excel 电子表格(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 功能
- 多工作表渲染,带工作表标题
- 单元格类型:字符串、整数、浮点数、布尔值、日期、错误
- 自动列宽计算
- 内容超出可用空间时自动分页
PowerPoint 演示文稿(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)?;
配置(Rust)
使用以下方式自定义页面大小、边距和字体: 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 字段
| 字段 | 类型 | 默认值 | 描述 |
|---|---|---|---|
page_size |
PageSize |
Letter | 页面尺寸 |
margins |
Margins |
1 inch all sides | 页面边距(磅) (72pt = 1 inch) |
embed_fonts |
bool |
false |
是否嵌入字体 |
default_font |
String |
"Helvetica" |
回退字体 |
default_font_size |
f32 |
11.0 |
默认文本大小(磅) |
line_height |
f32 |
1.2 |
行高倍数 |
include_images |
bool |
true |
包含嵌入的图片 |
页面大小预设
let config = OfficeConfig::letter(); // 8.5 × 11 inches (default)
let config = OfficeConfig::a4(); // 210 × 297 mm
自定义边距
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
批量转换
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 |
自动检测格式并转换 |
OfficeConverter.from_docx(path) |
Pdf |
转换 DOCX 文件 |
OfficeConverter.from_docx_bytes(data) |
Pdf |
从字节转换 DOCX |
OfficeConverter.from_xlsx(path) |
Pdf |
转换 XLSX 文件 |
OfficeConverter.from_xlsx_bytes(data) |
Pdf |
从字节转换 XLSX |
OfficeConverter.from_pptx(path) |
Pdf |
转换 PPTX 文件 |
OfficeConverter.from_pptx_bytes(data) |
Pdf |
从字节转换 PPTX |
所有方法均返回 Pdf 对象。调用 pdf.save("output.pdf") 或 pdf.to_bytes() 获取结果。
Rust — OfficeConverter
| 方法 | 返回值 | 描述 |
|---|---|---|
OfficeConverter::new() |
OfficeConverter |
使用默认配置创建 |
OfficeConverter::with_config(config) |
OfficeConverter |
使用自定义配置创建 |
convert(path) |
Result<Vec<u8>> |
自动检测格式并转换 |
convert_docx(path) |
Result<Vec<u8>> |
转换 DOCX 文件 |
convert_docx_bytes(bytes) |
Result<Vec<u8>> |
从字节转换 DOCX |
convert_xlsx(path) |
Result<Vec<u8>> |
转换 XLSX 文件 |
convert_xlsx_bytes(bytes) |
Result<Vec<u8>> |
从字节转换 XLSX |
convert_pptx(path) |
Result<Vec<u8>> |
转换 PPTX 文件 |
convert_pptx_bytes(bytes) |
Result<Vec<u8>> |
从字节转换 PPTX |
相关页面
- 从 Markdown 创建 — 将 Markdown 文本转换为 PDF
- 从 HTML 创建 — 将 HTML 转换为 PDF
- 从图片创建 — 将图片转换为 PDF
- 批量处理 — 并行处理模式