Limitations and API Differences
Overview
Aspose.Slides for Python via Java uses JPype to access the Java library from Python. The examples below compare package imports, presentation creation, and file handling in the two APIs.
Known Limitations
- JVM lifecycle: JPype supports one JVM per Python process. After shutting it down, you cannot restart it in the same process. Start it once and reuse it for subsequent presentation operations.
- Architecture compatibility: Python and Java must have matching architectures. See System Requirements for details.
See the JPype User Guide for details about these restrictions and Java interoperability.
Public API Differences
Compare the Java and Python examples below. For Python via Java member details, see the API Reference.
Import the Library
Java imports classes from com.aspose.slides. In Python, import asposeslides before starting the JVM, then import classes from asposeslides.api after the JVM is running. Use jpype.isJVMStarted to avoid starting an already running JVM.
Aspose.Slides for Java
import com.aspose.slides.Presentation;
import com.aspose.slides.SaveFormat;
Aspose.Slides for Python via Java
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import Presentation, SaveFormat
Note
The Python examples leave the JVM running until the Python process exits. In a notebook, reuse the active JVM across cells. If it has already been shut down, restart the notebook kernel before using Java objects again.Create a Presentation
Java uses the new keyword; Python calls the Presentation class directly. Release presentation resources with Presentation.dispose in a finally block.
Both examples save an empty presentation using Presentation.save and SaveFormat.Pptx.
Aspose.Slides for Java
import com.aspose.slides.Presentation;
import com.aspose.slides.SaveFormat;
Presentation presentation = new Presentation();
try {
presentation.save("new-presentation.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
Aspose.Slides for Python via Java
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import Presentation, SaveFormat
presentation = Presentation()
try:
presentation.save("new-presentation.pptx", SaveFormat.Pptx)
finally:
presentation.dispose()
Read Files and Use Format Constants
Java can load a presentation from a Java input stream. In Python, read the file as binary data and pass the resulting bytes to Presentation.createPresentationFromBytes. A Python file object is not a Java input stream.
The examples below require an existing presentation.pptx in the working directory and save a copy as result.pptx. Both close the input file and release presentation resources. The Python example reads the entire input file into memory.
Aspose.Slides for Java
import com.aspose.slides.Presentation;
import com.aspose.slides.SaveFormat;
import java.io.FileInputStream;
import java.io.InputStream;
try (InputStream inputStream = new FileInputStream("presentation.pptx")) {
Presentation presentation = new Presentation(inputStream);
try {
presentation.save("result.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
}
Aspose.Slides for Python via Java
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import Presentation, SaveFormat
with open("presentation.pptx", "rb") as input_file:
data = input_file.read()
presentation = Presentation.createPresentationFromBytes(data)
try:
presentation.save("result.pptx", SaveFormat.Pptx)
finally:
presentation.dispose()
FAQ
Do I need to restart the JVM for each presentation?
No. Keep the JVM running and create and dispose of presentation objects as needed. Shutting down the JVM prevents further Java operations in the same Python process.
Can I open a presentation directly from a file path?
Yes. The Presentation constructor accepts a file path. Use the byte-based helper when the presentation data is already available as Python bytes.
Should I change the format constant names when translating Java examples to Python?
No. For example, SaveFormat.Pptx uses the same spelling and capitalization in both APIs.