PbPPT Library

Version 1.8
Author: lcode.cn | License: Apache 2.0

Overview

PbPPT is a PureBasic library for creating and reading PowerPoint pptx files without requiring Microsoft Office or any third-party dependencies.

This library is ported from the Python python-pptx project (v1.0.2), using PureBasic's built-in XML and Packer libraries.

Note: Before using PbPPT, you need to add XIncludeFile "PbPPT.pb" at the beginning of your code and call PbPPT_Init() to initialize the library.

Key Features

Function Index

Presentation Operations

PbPPT_Init()
Initialize the PbPPT library. This function must be called before using any other PbPPT functions.
; Initialize the PbPPT library PbPPT_Init()
PbPPT_Create()
Create a new PowerPoint presentation. After creation, you can add slides and shapes.
Returns: #True on success, #False on failure.
PbPPT_Init() If PbPPT_Create() ; Presentation created successfully PbPPT_AddSlide(0) PbPPT_Save("output.pptx") PbPPT_Close() EndIf
PbPPT_Open(filename.s)
Open an existing PowerPoint file. You can read file information and modify it before saving.
ParameterTypeDescription
filenameStringFull path to the pptx file
Returns: #True on success, #False on failure.
PbPPT_Save(filename.s)
Save the presentation as a pptx file. Generates a file compliant with the Office Open XML standard.
ParameterTypeDescription
filenameStringFull path for the output file (.pptx extension)
Returns: #True on success, #False on failure.
If PbPPT_Save("output.pptx") MessageRequester("Success", "File saved!") Else MessageRequester("Error", "Save failed!") EndIf
PbPPT_Close()
Close the current presentation and release all associated resources. Should be called after saving.
PbPPT_GetSlideCount()
Get the number of slides in the current presentation.
Returns: Number of slides (integer).

Slide Operations

PbPPT_AddSlide(layoutIndex.i)
Add a new slide to the end of the presentation.
ParameterTypeDescription
layoutIndexIntegerLayout index (currently only 0=blank layout)
Returns: Slide index (integer), starting from 0.
; Add a blank slide slideIdx = PbPPT_AddSlide(0)
PbPPT_SetSlideWidth(width.i)
Set the slide width. Unit is EMU (English Metric Units).
ParameterTypeDescription
widthIntegerSlide width (EMU)
PbPPT_SetSlideHeight(height.i)
Set the slide height. Unit is EMU.
PbPPT_GetSlideWidth()
Get the current slide width (EMU).
Returns: Slide width (integer, EMU).
PbPPT_GetSlideHeight()
Get the current slide height (EMU).
Returns: Slide height (integer, EMU).
Common slide dimension constants:
#PbPPT_STANDARD_SLIDE_WIDTH = 9144000 (10 inches, standard 4:3)
#PbPPT_STANDARD_SLIDE_HEIGHT = 6858000 (7.5 inches, standard 4:3)
#PbPPT_WIDESCREEN_SLIDE_WIDTH = 12192000 (13.333 inches, widescreen 16:9)
#PbPPT_WIDESCREEN_SLIDE_HEIGHT = 6858000 (7.5 inches, widescreen 16:9)

Shape Operations

PbPPT_AddTextbox(slideIndex.i, left.i, top.i, width.i, height.i)
Add a text box on the specified slide.
ParameterTypeDescription
slideIndexIntegerSlide index (starting from 0)
leftIntegerLeft offset (EMU)
topIntegerTop offset (EMU)
widthIntegerText box width (EMU)
heightIntegerText box height (EMU)
Returns: Shape ID (integer), used for subsequent operations.
tbId = PbPPT_AddTextbox(0, PbPPT_InchesToEmu(1), PbPPT_InchesToEmu(1), PbPPT_InchesToEmu(8), PbPPT_InchesToEmu(2)) PbPPT_SetShapeText(tbId, "Hello PbPPT!")
PbPPT_AddShape(slideIndex.i, prstGeom.s, left.i, top.i, width.i, height.i)
Add an auto shape on the specified slide.
ParameterTypeDescription
slideIndexIntegerSlide index
prstGeomStringPreset shape name, e.g. #PbPPT_PrstRectangle$
leftIntegerLeft offset (EMU)
topIntegerTop offset (EMU)
widthIntegerShape width (EMU)
heightIntegerShape height (EMU)
Returns: Shape ID (integer).
; Add a blue rectangle rectId = PbPPT_AddShape(0, #PbPPT_PrstRectangle$, PbPPT_InchesToEmu(2), PbPPT_InchesToEmu(3), PbPPT_InchesToEmu(6), PbPPT_InchesToEmu(2)) PbPPT_SetFillSolid(rectId, "4472C4")
Common preset shape constants:
#PbPPT_PrstRectangle$ - Rectangle
#PbPPT_PrstRoundRectangle$ - Rounded Rectangle
#PbPPT_PrstEllipse$ - Ellipse/Circle
#PbPPT_PrstTriangle$ - Triangle
#PbPPT_PrstDiamond$ - Diamond
#PbPPT_PrstStar5$ - 5-Point Star
#PbPPT_PrstArrow$ - Arrow
PbPPT_AddConnector(slideIndex.i, beginX.i, beginY.i, endX.i, endY.i)
Add a straight-line connector on the specified slide.
ParameterTypeDescription
slideIndexIntegerSlide index
beginXIntegerStart X coordinate (EMU)
beginYIntegerStart Y coordinate (EMU)
endXIntegerEnd X coordinate (EMU)
endYIntegerEnd Y coordinate (EMU)
Returns: Shape ID (integer).

Text Operations

PbPPT_SetShapeText(shapeId.i, text.s)
Set the text content of a shape. Use line breaks (#LF$) to separate multiple paragraphs.
ParameterTypeDescription
shapeIdIntegerShape ID
textStringText content, #LF$ separates paragraphs
; Set multi-paragraph text PbPPT_SetShapeText(tbId, "Paragraph 1" + #LF$ + "Paragraph 2" + #LF$ + "Paragraph 3")
PbPPT_SetFont(shapeId.i, paraIdx.i, runIdx.i, name.s, size.i, bold.b, italic.b, underline.b, color.s)
Set the font style of text in a shape.
ParameterTypeDescription
shapeIdIntegerShape ID
paraIdxIntegerParagraph index (starting from 1)
runIdxIntegerRun index (starting from 1)
nameStringFont name, e.g. "Arial"
sizeIntegerFont size (points), e.g. 28
boldBooleanBold
italicBooleanItalic
underlineBooleanUnderline
colorStringFont color (6-digit hex), e.g. "FF0000"
; Set paragraph 1, run 1 to Arial 28pt bold blue PbPPT_SetFont(tbId, 1, 1, "Arial", 28, #True, #False, #False, "2E75B6")
PbPPT_AddRun(shapeId.i, paraIdx.i, text.s, name.s, size.i, bold.b, italic.b, underline.b, color.s)
Add a rich text Run at the end of the specified paragraph, enabling different styles within the same paragraph.
ParameterTypeDescription
shapeIdIntegerShape ID
paraIdxIntegerParagraph index (starting from 1)
textStringRun text content
nameStringFont name
sizeIntegerFont size (points)
boldBooleanBold
italicBooleanItalic
underlineBooleanUnderline
colorStringFont color (6-digit hex)
; Add a red bold Run PbPPT_AddRun(tbId, 1, "(red bold)", "Arial", 14, #True, #False, #False, "FF0000")
PbPPT_SetParagraphAlignment(shapeId.i, paraIdx.i, align.i)
Set paragraph alignment.
ParameterTypeDescription
shapeIdIntegerShape ID
paraIdxIntegerParagraph index (starting from 1)
alignIntegerAlignment constant
Alignment constants:
#PbPPT_AlignLeft = 0 - Left align
#PbPPT_AlignCenter = 1 - Center
#PbPPT_AlignRight = 2 - Right align
#PbPPT_AlignJustify = 3 - Justify

Style Operations

PbPPT_SetFillSolid(shapeId.i, color.s)
Set a solid fill for the shape.
ParameterTypeDescription
shapeIdIntegerShape ID
colorStringFill color (6-digit hex), e.g. "4472C4"
PbPPT_SetFillSolid(shapeId, "4472C4") ; Blue fill
PbPPT_SetFillNone(shapeId.i)
Set the shape to have no fill (transparent).
PbPPT_SetLineFormat(shapeId.i, width.i, color.s)
Set the line format for the shape.
ParameterTypeDescription
shapeIdIntegerShape ID
widthIntegerLine width (EMU), use PbPPT_PtToEmu to convert
colorStringLine color (6-digit hex)
; Set 1pt dark blue line PbPPT_SetLineFormat(shapeId, PbPPT_PtToEmu(1), "2F528F")
PbPPT_SetShapeRotation(shapeId.i, angle.f)
Set the rotation angle of the shape.
ParameterTypeDescription
shapeIdIntegerShape ID
angleFloatRotation angle (degrees), positive=clockwise, negative=counter-clockwise
PbPPT_SetShapeRotation(shapeId, 45) ; Rotate 45 degrees clockwise PbPPT_SetShapeRotation(shapeId, -15) ; Rotate 15 degrees counter-clockwise

Table Operations

PbPPT_AddTable(slideIndex.i, rows.i, cols.i, left.i, top.i, width.i, height.i)
Add a table on the specified slide.
ParameterTypeDescription
slideIndexIntegerSlide index
rowsIntegerNumber of rows
colsIntegerNumber of columns
leftIntegerLeft offset (EMU)
topIntegerTop offset (EMU)
widthIntegerTable width (EMU)
heightIntegerTable height (EMU)
Returns: Shape ID (integer).
tblId = PbPPT_AddTable(0, 4, 3, PbPPT_InchesToEmu(1), PbPPT_InchesToEmu(2), PbPPT_InchesToEmu(8), PbPPT_InchesToEmu(3)) PbPPT_SetCellValue(tblId, 1, 1, "Name") PbPPT_SetCellValue(tblId, 1, 2, "Age")
PbPPT_SetCellValue(shapeId.i, row.i, col.i, text.s)
Set the value of a table cell.
ParameterTypeDescription
shapeIdIntegerTable shape ID
rowIntegerRow number (starting from 1)
colIntegerColumn number (starting from 1)
textStringCell text
PbPPT_GetCellValue(shapeId.i, row.i, col.i)
Get the value of a table cell.
Returns: Cell text (string).
PbPPT_SetColWidth(shapeId.i, col.i, width.i)
Set the column width of a table.
PbPPT_SetRowHeight(shapeId.i, row.i, height.i)
Set the row height of a table.

Chart Operations

PbPPT_AddChart(slideIndex.i, chartType.i, left.i, top.i, width.i, height.i)
Add a chart on the specified slide.
ParameterTypeDescription
slideIndexIntegerSlide index
chartTypeIntegerChart type constant
leftIntegerLeft offset (EMU)
topIntegerTop offset (EMU)
widthIntegerChart width (EMU)
heightIntegerChart height (EMU)
Returns: Shape ID (integer).
Chart type constants:
#PbPPT_ChartTypeColumnClustered = 1 - Clustered Column
#PbPPT_ChartTypeColumnStacked = 2 - Stacked Column
#PbPPT_ChartTypeBarClustered = 4 - Clustered Bar
#PbPPT_ChartTypeLine = 8 - Line
#PbPPT_ChartTypeLineMarkers = 9 - Line with Markers
#PbPPT_ChartTypePie = 10 - Pie
#PbPPT_ChartTypePieExploded = 11 - Exploded Pie
#PbPPT_ChartTypeDoughnut = 14 - Doughnut
#PbPPT_ChartTypeRadar = 15 - Radar
PbPPT_SetChartTitle(shapeId.i, title.s)
Set the chart title.
PbPPT_AddChartCategory(shapeId.i, label.s)
Add a chart category label.
PbPPT_AddChartCategory(chartId, "Q1") PbPPT_AddChartCategory(chartId, "Q2")
PbPPT_AddChartSeries(shapeId.i, name.s, values.s)
Add a chart data series.
ParameterTypeDescription
shapeIdIntegerChart shape ID
nameStringSeries name
valuesStringSeries values, comma-separated, e.g. "120,200,150"
PbPPT_AddChartSeries(chartId, "Product A", "120,200,150,300") PbPPT_AddChartSeries(chartId, "Product B", "80,150,220,250")
PbPPT_SetChartHasLegend(shapeId.i, hasLegend.b)
Set whether the chart displays a legend.
PbPPT_SetChartStyle(shapeId.i, style.i)
Set the chart style number.

Picture Operations

PbPPT_AddPicture(slideIndex.i, imagePath.s, left.i, top.i, width.i, height.i)
Add a picture on the specified slide. Supports PNG, JPG, BMP, GIF formats. When width and height are 0, the image size is auto-detected.
ParameterTypeDescription
slideIndexIntegerSlide index
imagePathStringFull path to the image file
leftIntegerLeft offset (EMU)
topIntegerTop offset (EMU)
widthIntegerPicture width (EMU), 0 for auto-detect
heightIntegerPicture height (EMU), 0 for auto-detect
Returns: Shape ID (integer).
; Add picture with specified size picId = PbPPT_AddPicture(0, "photo.png", PbPPT_InchesToEmu(1), PbPPT_InchesToEmu(2), PbPPT_InchesToEmu(4), PbPPT_InchesToEmu(3)) ; Add picture with auto size picId2 = PbPPT_AddPicture(0, "logo.png", PbPPT_InchesToEmu(5), PbPPT_InchesToEmu(1), 0, 0)
Note: Identical image files are automatically deduplicated, storing only one copy in the PPTX to reduce file size.

Document Properties

PbPPT_SetTitle(title.s)
Set the document title.
PbPPT_SetAuthor(author.s)
Set the document author.
PbPPT_SetSubject(subject.s)
Set the document subject.
PbPPT_GetTitle()
Get the document title.
Returns: Document title (string).
PbPPT_GetAuthor()
Get the document author.
Returns: Document author (string).

Unit Conversion

PbPPT uses EMU (English Metric Units) as its internal unit. 1 inch = 914400 EMU, 1 cm = 360000 EMU, 1 point = 12700 EMU.

PbPPT_InchesToEmu(inches.f)
Convert inches to EMU.
emu = PbPPT_InchesToEmu(1.0) ; Returns 914400 emu = PbPPT_InchesToEmu(2.5) ; Returns 2286000
PbPPT_CmToEmu(cm.f)
Convert centimeters to EMU.
emu = PbPPT_CmToEmu(2.54) ; Returns 914400 (1 inch)
PbPPT_PtToEmu(pt.f)
Convert points to EMU.
emu = PbPPT_PtToEmu(1.0) ; Returns 12700
PbPPT_EmuToInches(emu.i)
Convert EMU to inches.
Returns: Inches value (float).

Example Code

Example 1: Create a Presentation with Text and Shapes

XIncludeFile "PbPPT.pb" PbPPT_Init() If PbPPT_Create() slideIdx = PbPPT_AddSlide(0) ; Add title titleId = PbPPT_AddTextbox(slideIdx, PbPPT_InchesToEmu(1), PbPPT_InchesToEmu(0.5), PbPPT_InchesToEmu(8), PbPPT_InchesToEmu(1.5)) PbPPT_SetShapeText(titleId, "PbPPT Example") PbPPT_SetFont(titleId, 1, 1, "Arial", 28, #True, #False, #False, "2E75B6") ; Add rectangle rectId = PbPPT_AddShape(slideIdx, #PbPPT_PrstRectangle$, PbPPT_InchesToEmu(2), PbPPT_InchesToEmu(3), PbPPT_InchesToEmu(6), PbPPT_InchesToEmu(2)) PbPPT_SetFillSolid(rectId, "4472C4") PbPPT_SetShapeText(rectId, "Blue Rectangle") PbPPT_SetFont(rectId, 1, 1, "Arial", 18, #True, #False, #False, "FFFFFF") PbPPT_Save("output.pptx") PbPPT_Close() EndIf

Example 2: Create a Presentation with a Chart

XIncludeFile "PbPPT.pb" PbPPT_Init() If PbPPT_Create() slideIdx = PbPPT_AddSlide(0) ; Add column chart chartId = PbPPT_AddChart(slideIdx, #PbPPT_ChartTypeColumnClustered, PbPPT_InchesToEmu(0.5), PbPPT_InchesToEmu(1.5), PbPPT_InchesToEmu(9), PbPPT_InchesToEmu(5.5)) PbPPT_SetChartTitle(chartId, "Quarterly Sales") ; Add categories PbPPT_AddChartCategory(chartId, "Q1") PbPPT_AddChartCategory(chartId, "Q2") PbPPT_AddChartCategory(chartId, "Q3") PbPPT_AddChartCategory(chartId, "Q4") ; Add data series PbPPT_AddChartSeries(chartId, "Product A", "120,200,150,300") PbPPT_AddChartSeries(chartId, "Product B", "80,150,220,250") PbPPT_SetChartHasLegend(chartId, #True) PbPPT_Save("chart_output.pptx") PbPPT_Close() EndIf

Version History

v1.8 (2026-04-24)

v1.7 (2026-04-24)

v1.6 (2026-04-23)

v1.5 (2026-04-22)

v1.4 (2026-04-21)

v1.3 (2026-04-20)

v1.2 (2026-04-19)

v1.1 (2026-04-18)

v1.0 (2026-04-17)

Support This Project

If PbPPT is helpful to you, consider supporting the developer's ongoing maintenance and updates:

PayPal: https://www.paypal.me/lcodecn

WeChat Pay: #付款:lcodecn(经营_lcodecn)/openlib/003

Your support is the driving force for the continued development of open-source projects!