How to Capture All Slides as PNG Screenshots Using OfficeCLI: 2 Proven Methods
OfficeCLI converts PowerPoint presentations to PNG images through a dedicated screenshot view mode that supports both individual slide extraction and bulk contact-sheet generation.
OfficeCLI enables headless conversion of .pptx files to raster images without requiring Microsoft PowerPoint to be installed. Whether you need separate image files for each slide or a single tiled overview, the tool's native rendering pipeline provides precise control over output dimensions and formatting.
How the Screenshot Engine Works
When you invoke the screenshot command, the request enters src/officecli/ResidentServer.cs at lines 1553–1559, where the server detects the screenshot verb and routes to the dedicated handler. The implementation follows a four-stage pipeline:
- Page Resolution: If no
--pageargument is provided, the system defaults to slide 1 (lines 1565–1569). To capture multiple slides, you must explicitly iterate through pages or use the grid mode. - Dimension Detection: The handler queries native slide dimensions via
PowerPointHandler.GetSlideNativePixels()(lines 1584–1586) to ensure the PNG matches the original aspect ratio. - Rendering Dispatch: For single slides, the code calls
PowerPointPngBackend.Render; for grid layouts, it invokesRenderGrid(lines 1616–1619). - Output Writing: The resulting image stream writes to the path specified by
-o, or to a temporary file if omitted (lines 1731–1735).
This architecture relies on src/officecli/Handlers/Pptx/PowerPointHandler.cs to abstract slide metadata, while PowerPointPngBackend.cs handles the actual rasterization—using native Windows APIs when available or falling back to HTML-to-PNG conversion via Playwright on other platforms.
Method 1: Export Individual PNGs for Each Slide
To generate one PNG file per slide, combine the stats view with a shell loop. First, query the total slide count, then invoke the screenshot view for each page index:
slide_cnt=$(officecli view deck.pptx stats --json | jq .slideCount)
for i in $(seq 1 $slide_cnt); do
officecli view deck.pptx screenshot --page $i -o "./slides/slide_${i}.png"
done
echo "Saved $slide_cnt PNG files in ./slides/"
Each iteration calls --page $i to target a specific slide, producing sequentially numbered files like slide_1.png, slide_2.png, etc. This approach preserves maximum resolution and allows per-slide processing in downstream workflows.
Method 2: Generate a Single Contact-Sheet PNG
For documentation thumbnails or quick overviews, export all slides into one tiled image using the --grid parameter. Setting --grid -1 instructs the renderer to auto-calculate the optimal column count:
officecli view deck.pptx screenshot --grid -1 -o "./deck_all.png"
echo "Saved contact-sheet PNG to ./deck_all.png"
As implemented in ResidentServer.cs at lines 1602–1609, the -1 value triggers automatic column detection, tiling every slide into a single composite image. If you require a fixed layout—such as four columns—substitute -1 with your desired number (e.g., --grid 4).
Key Source Files and Implementation Details
Understanding the codebase helps troubleshoot rendering issues:
src/officecli/ResidentServer.cs: Contains the CLI argument parser and orchestration logic for the screenshot verb, including--pageand--gridhandling.src/officecli/Handlers/Pptx/PowerPointHandler.cs: Provides slide-count queries and native pixel dimensions viaGetSlideNativePixels().src/officecli/Core/PowerPointPngBackend.cs: Implements the low-level PNG export, managing both native Windows GDI+ rendering and headless browser fallback.
The PNG backend references PowerPointHandler.HtmlPreview.cs when generating intermediate HTML for non-Windows environments, ensuring consistent output across operating systems.
Summary
- OfficeCLI provides native screenshot capabilities through the
view <file> screenshotcommand structure. - Individual exports require iterating with
--pageflags after determining slide count via thestatsview. - Contact-sheet generation uses
--grid -1to tile all slides into one PNG file automatically. - The rendering pipeline depends on
ResidentServer.csfor CLI parsing andPowerPointPngBackend.csfor image generation. - Native slide dimensions are preserved by querying
PowerPointHandler.GetSlideNativePixels()before rasterization.
Frequently Asked Questions
How do I capture all slides at once without writing a loop?
Use the contact-sheet approach with --grid -1. This command renders every slide into a single tiled PNG: officecli view deck.pptx screenshot --grid -1 -o all_slides.png. The -1 value triggers automatic column calculation based on slide count.
What determines the resolution of the output PNGs?
The resolution matches the native slide dimensions returned by PowerPointHandler.GetSlideNativePixels() in PowerPointHandler.cs. This ensures the PNG reflects the original presentation's aspect ratio and pixel density without artificial scaling.
Can I use OfficeCLI to convert slides to PNG on Linux or macOS?
Yes. While PowerPointPngBackend.cs prefers native Windows APIs for rendering, it automatically falls back to HTML-to-PNG conversion using Playwright on non-Windows platforms. The PowerPointHandler.HtmlPreview.cs file generates the intermediate HTML representation required for this cross-platform compatibility.
Why does the screenshot command default to slide 1 when I omit the --page flag?
The handler in ResidentServer.cs (lines 1565–1569) explicitly defaults the page parameter to 1 if no --page argument is detected. This safety measure prevents accidental bulk operations and ensures deterministic output when running simple commands.
Have a question about this repo?
These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:
curl -s "https://instagit.com/install.md" Maintain an open-source project? Get it listed too →