How to retrieve all built-in templates in a PPT via API and implement the functionality of creating a PPT based on a selected template

Hello OnlyOffice team, I have a question to ask.

As mentioned, I’ve noticed that both OnlyOffice and Microsoft Office support creating slides based on built-in PPT templates, as shown in the example image.

Is there a way to achieve the following operations via JavaScript API?

  1. List all templates included in a PPT (to allow AI to generate content matching the selected template).
  2. Create a PPT slide based on a selected template via JS API.
  3. Modify editable text placeholders within the template via JS API.

I’m currently researching the feasibility of developing a Macro that allows users to:

  • Select a PPT template,
  • Use our internally deployed AI application (via web API) to generate PPT content,
  • Automatically populate the AI-generated content into the template.

To complete this workflow, I need to address the following:

  1. How to list all templates in a PPT via JS API.
  2. How to create slides based on selected templates via JS API.
  3. How to modify editable text in templates via JS API.

After reviewing the API documentation, I haven’t found relevant APIs for these requirements. Could you clarify which of these functionalities are currently supported by the JS API?

Looking forward to your response.

----------------------- conteent translated by deepseek -----------------------

如何通过 API 获取 PPT 中内置的所有模板,并且实现用户选中某个模板后根据模板创建 PPT 功能。

OnlyOffice 的朋友们你们好,有个问题想请教下。

如题,目前我发现 OnlyOffice 和微软的 Office 都支持根据 PPT 模板中内置的模板创建页面功能,具体如下图所示。
列出 PPT 包含的所用模板,并且根据选中的模板创建页面的操作有办法通过 JS API 来完成吗?目前我在调研一个需求
的可行性,要求开发一个 Macro,用户选择一个 PPT 模板后,通过 web api 用我们自己在公司内部署的 AI 应用生成
PPT 内容,然后将 AI 返回的内容自动填充模板。

要完成这个流程需要解决如下几个问题:

  1. 如何通过 JS API 列出 PPT 内包含的模板 (因为后面要根据选择的模板页面让 AI 来生成匹配的内容)
  2. 如何通过 JS API 根据模板创建 PPT 页面。
  3. 如何通过 JS API 修改模板中设置的可编辑文本。

我自己查了相关的 API,暂时还没有找到符合要求的 API,所以我想问下,我期望的这些功能目前那些是有对应的 JS API
可以支持的。

期待您的答复。

Hello @isNaN

What you are describing as templates, are layouts. There is complete API class for layouts: ApiLayout | ONLYOFFICE
To change slide layout, you can use ApplyLayout method. If you need to create a slide and then apply specific layout to the slide, then using CreateSlide is necessary first. Getting specific layout from the master is performed with GetLayout method. All layouts are indexed as they are positioned in the master.
Basically, all above convers first to items in your list: you can get specific layouts, then you can create a slide and apply the layout you want.

Tricky part is to fill the text areas with actual data. Basically, each text area is a shape, you can refer to the them as ApiShape class. This class has 1 major method, which is GetContent. This method gets the content of necessary shape and allows modifying it. Depending on how you would proceed, you can either create a slide with earlier mentioned method, then get all shapes on the slide with GetAllShapes, get content of each shape separately, modify the content and add the slide, or you can get existing slide with any suitable method, e.g. GetSlideByIndex, then get all shapes, get content of these shapes one by one and modify it.

Pretty much everything is available, but I’ve shared very basic way of doing that. Depending on how complex your final scenario is, you can proceed in accordance with classes I’ve mentioned and methods available for them.

Thank you very much for your reply. The information you provided is very helpful to me.

1 Like

I tried the APIs you recommended, and they mostly meet my needs. However, I am currently facing an issue that confuses me. The Api.ApplyLayout method throws an exception:

“An error occurred during the work with the document. Use the ‘Download as’ option to save the file backup copy to a drive.”
Details are as shown in the image below:

Reproduction steps:

  • Enter the following code and run:
var Api = editor;
var ppt = Api.GetPresentation();
var master = ppt.GetMaster(0);
var oSlide = Api.CreateSlide();
var layout = master.GetLayout(1);

oSlide.ApplyLayout(layout); // will throw an error
ppt.AddSlide(oSlide);

That is improper way to test out Office API methods. Please use following site: Playground | ONLYOFFICE

You code without unnecessary var Api = editor; works as expected.