Merge pull request #202 from presenton/fix/pptx-export-issues

fix: division by zero, no paragraphs in text box model and elements with 0 width or height issue solved
This commit is contained in:
Saurav Niraula 2025-08-10 17:57:56 +05:45 committed by GitHub
commit 9bd97d22ec
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 19 additions and 3 deletions

View file

@ -1,5 +1,5 @@
from enum import Enum
from typing import Annotated, List, Optional
from typing import Annotated, List, Literal, Optional
from annotated_types import Len
from pydantic import BaseModel
from pptx.util import Pt
@ -105,10 +105,11 @@ class PptxPictureModel(BaseModel):
class PptxShapeModel(BaseModel):
pass
shape_type: Literal["textbox", "autoshape", "picture", "connector"]
class PptxTextBoxModel(PptxShapeModel):
shape_type: Literal["textbox"] = "textbox"
margin: Optional[PptxSpacingModel] = None
fill: Optional[PptxFillModel] = None
position: PptxPositionModel
@ -117,6 +118,7 @@ class PptxTextBoxModel(PptxShapeModel):
class PptxAutoShapeBoxModel(PptxShapeModel):
shape_type: Literal["autoshape"] = "autoshape"
type: MSO_AUTO_SHAPE_TYPE = MSO_AUTO_SHAPE_TYPE.RECTANGLE
margin: Optional[PptxSpacingModel] = None
fill: Optional[PptxFillModel] = None
@ -129,6 +131,7 @@ class PptxAutoShapeBoxModel(PptxShapeModel):
class PptxPictureBoxModel(PptxShapeModel):
shape_type: Literal["picture"] = "picture"
position: PptxPositionModel
margin: Optional[PptxSpacingModel] = None
clip: bool = True
@ -141,6 +144,7 @@ class PptxPictureBoxModel(PptxShapeModel):
class PptxConnectorModel(PptxShapeModel):
shape_type: Literal["connector"] = "connector"
type: MSO_CONNECTOR_TYPE = MSO_CONNECTOR_TYPE.STRAIGHT
position: PptxPositionModel
thickness: float = 0.5

View file

@ -179,7 +179,7 @@ const convertSvgToPng = async (element_attibutes: ElementAttributes) => {
const svgBuffer = Buffer.from(svgHtml);
const pngBuffer = await sharp(svgBuffer)
.resize(Math.round(element_attibutes.position?.width ?? 10), Math.round(element_attibutes.position?.height ?? 10))
.resize(Math.round(element_attibutes.position!.width!), Math.round(element_attibutes.position!.height!))
.toFormat('png')
.toBuffer();
return pngBuffer;
@ -261,6 +261,10 @@ async function getAllChildElementsAttributes({ element, rootRect = null, depth =
};
}
if (attributes.position === undefined || attributes.position.width === undefined || attributes.position.height === undefined || attributes.position.width === 0 || attributes.position.height === 0) {
continue;
}
if (attributes.tagName === 'svg' || attributes.tagName === 'canvas' || attributes.tagName === 'table') {
attributes.should_screenshot = true;
attributes.element = childElementHandle;

View file

@ -281,6 +281,7 @@ export interface PptxShapeModel {
}
export interface PptxTextBoxModel extends PptxShapeModel {
shape_type: string;
margin?: PptxSpacingModel;
fill?: PptxFillModel;
position: PptxPositionModel;
@ -289,6 +290,7 @@ export interface PptxTextBoxModel extends PptxShapeModel {
}
export interface PptxAutoShapeBoxModel extends PptxShapeModel {
shape_type: string;
type?: PptxShapeType;
margin?: PptxSpacingModel;
fill?: PptxFillModel;
@ -301,6 +303,7 @@ export interface PptxAutoShapeBoxModel extends PptxShapeModel {
}
export interface PptxPictureBoxModel extends PptxShapeModel {
shape_type: string;
position: PptxPositionModel;
margin?: PptxSpacingModel;
clip: boolean;
@ -313,6 +316,7 @@ export interface PptxPictureBoxModel extends PptxShapeModel {
}
export interface PptxConnectorModel extends PptxShapeModel {
shape_type: string;
type?: PptxConnectorType;
position: PptxPositionModel;
thickness: number;

View file

@ -132,6 +132,7 @@ function convertToTextBox(element: ElementAttributes): PptxTextBoxModel {
};
return {
shape_type: "textbox",
margin: undefined,
fill,
position,
@ -190,6 +191,7 @@ function convertToAutoShapeBox(element: ElementAttributes): PptxAutoShapeBoxMode
}
return {
shape_type: "autoshape",
type: shapeType,
margin: undefined,
fill,
@ -220,6 +222,7 @@ function convertToPictureBox(element: ElementAttributes): PptxPictureBoxModel {
};
return {
shape_type: "picture",
position,
margin: undefined,
clip: element.clip ?? true,
@ -241,6 +244,7 @@ function convertToConnector(element: ElementAttributes): PptxConnectorModel {
};
return {
shape_type: "connector",
type: PptxConnectorType.STRAIGHT,
position,
thickness: element.border?.width ?? 0.5,