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
- Create and read PowerPoint pptx files
- Add text boxes, auto shapes, pictures, tables, charts, connectors
- Set font styles (name, size, color, bold, italic, underline)
- Set shape styles (solid fill, no fill, line format, rotation)
- Multi-paragraph text and rich text Run support
- Table cell read/write, row height and column width settings
- Chart data series and category label settings
- Document property settings (title, author, subject, etc.)
- Slide dimension settings (standard 4:3, widescreen 16:9, custom)
- Image deduplication to reduce file size
Presentation Operations
PbPPT_Init()
Initialize the PbPPT library. This function must be called before using any other PbPPT functions.
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()
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.
| Parameter | Type | Description |
| filename | String | Full 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.
| Parameter | Type | Description |
| filename | String | Full 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.
| Parameter | Type | Description |
| layoutIndex | Integer | Layout index (currently only 0=blank layout) |
Returns: Slide index (integer), starting from 0.
slideIdx = PbPPT_AddSlide(0)
PbPPT_SetSlideWidth(width.i)
Set the slide width. Unit is EMU (English Metric Units).
| Parameter | Type | Description |
| width | Integer | Slide 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.
| Parameter | Type | Description |
| slideIndex | Integer | Slide index (starting from 0) |
| left | Integer | Left offset (EMU) |
| top | Integer | Top offset (EMU) |
| width | Integer | Text box width (EMU) |
| height | Integer | Text 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.
| Parameter | Type | Description |
| slideIndex | Integer | Slide index |
| prstGeom | String | Preset shape name, e.g. #PbPPT_PrstRectangle$ |
| left | Integer | Left offset (EMU) |
| top | Integer | Top offset (EMU) |
| width | Integer | Shape width (EMU) |
| height | Integer | Shape height (EMU) |
Returns: Shape ID (integer).
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.
| Parameter | Type | Description |
| slideIndex | Integer | Slide index |
| beginX | Integer | Start X coordinate (EMU) |
| beginY | Integer | Start Y coordinate (EMU) |
| endX | Integer | End X coordinate (EMU) |
| endY | Integer | End 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.
| Parameter | Type | Description |
| shapeId | Integer | Shape ID |
| text | String | Text content, #LF$ separates paragraphs |
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.
| Parameter | Type | Description |
| shapeId | Integer | Shape ID |
| paraIdx | Integer | Paragraph index (starting from 1) |
| runIdx | Integer | Run index (starting from 1) |
| name | String | Font name, e.g. "Arial" |
| size | Integer | Font size (points), e.g. 28 |
| bold | Boolean | Bold |
| italic | Boolean | Italic |
| underline | Boolean | Underline |
| color | String | Font color (6-digit hex), e.g. "FF0000" |
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.
| Parameter | Type | Description |
| shapeId | Integer | Shape ID |
| paraIdx | Integer | Paragraph index (starting from 1) |
| text | String | Run text content |
| name | String | Font name |
| size | Integer | Font size (points) |
| bold | Boolean | Bold |
| italic | Boolean | Italic |
| underline | Boolean | Underline |
| color | String | Font color (6-digit hex) |
PbPPT_AddRun(tbId, 1, "(red bold)", "Arial", 14, #True, #False, #False, "FF0000")
PbPPT_SetParagraphAlignment(shapeId.i, paraIdx.i, align.i)
Set paragraph alignment.
| Parameter | Type | Description |
| shapeId | Integer | Shape ID |
| paraIdx | Integer | Paragraph index (starting from 1) |
| align | Integer | Alignment 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.
| Parameter | Type | Description |
| shapeId | Integer | Shape ID |
| color | String | Fill color (6-digit hex), e.g. "4472C4" |
PbPPT_SetFillSolid(shapeId, "4472C4")
PbPPT_SetFillNone(shapeId.i)
Set the shape to have no fill (transparent).
PbPPT_SetShapeRotation(shapeId.i, angle.f)
Set the rotation angle of the shape.
| Parameter | Type | Description |
| shapeId | Integer | Shape ID |
| angle | Float | Rotation angle (degrees), positive=clockwise, negative=counter-clockwise |
PbPPT_SetShapeRotation(shapeId, 45)
PbPPT_SetShapeRotation(shapeId, -15)
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.
| Parameter | Type | Description |
| slideIndex | Integer | Slide index |
| rows | Integer | Number of rows |
| cols | Integer | Number of columns |
| left | Integer | Left offset (EMU) |
| top | Integer | Top offset (EMU) |
| width | Integer | Table width (EMU) |
| height | Integer | Table 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.
| Parameter | Type | Description |
| shapeId | Integer | Table shape ID |
| row | Integer | Row number (starting from 1) |
| col | Integer | Column number (starting from 1) |
| text | String | Cell 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.
| Parameter | Type | Description |
| slideIndex | Integer | Slide index |
| chartType | Integer | Chart type constant |
| left | Integer | Left offset (EMU) |
| top | Integer | Top offset (EMU) |
| width | Integer | Chart width (EMU) |
| height | Integer | Chart 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.
| Parameter | Type | Description |
| shapeId | Integer | Chart shape ID |
| name | String | Series name |
| values | String | Series 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.
| Parameter | Type | Description |
| slideIndex | Integer | Slide index |
| imagePath | String | Full path to the image file |
| left | Integer | Left offset (EMU) |
| top | Integer | Top offset (EMU) |
| width | Integer | Picture width (EMU), 0 for auto-detect |
| height | Integer | Picture height (EMU), 0 for auto-detect |
Returns: Shape ID (integer).
picId = PbPPT_AddPicture(0, "photo.png", PbPPT_InchesToEmu(1), PbPPT_InchesToEmu(2), PbPPT_InchesToEmu(4), PbPPT_InchesToEmu(3))
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.
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)
emu = PbPPT_InchesToEmu(2.5)
PbPPT_CmToEmu(cm.f)
Convert centimeters to EMU.
emu = PbPPT_CmToEmu(2.54)
PbPPT_PtToEmu(pt.f)
Convert points to EMU.
emu = PbPPT_PtToEmu(1.0)
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)
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")
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)
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")
PbPPT_AddChartCategory(chartId, "Q1")
PbPPT_AddChartCategory(chartId, "Q2")
PbPPT_AddChartCategory(chartId, "Q3")
PbPPT_AddChartCategory(chartId, "Q4")
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
Support This Project
If PbPPT is helpful to you, consider supporting the developer's ongoing maintenance and updates:
Your support is the driving force for the continued development of open-source projects!