Presentations¶
A presentation is opened using the Presentation()
function, provided
directly by the pptx
package:
from pptx import Presentation
This function returns a Presentation
object which is the root of
a graph containing the components that constitute a presentation, e.g.
slides, shapes, etc. All existing presentation components are referenced by
traversing the graph and new objects are added to the graph by calling
a method on that object’s container. Consequently, python-pptx objects are generally
not constructed directly.
Example:
# load a presentation
prs = Presentation(path_to_pptx_file)
# get reference to first shape in first slide
sp = prs.slides[0].shapes[0]
# add a picture shape to slide
pic = sld.shapes.add_picture(path, x, y, cx, cy)
Presentation
function¶
This function is the only reference that must be imported to work with presentation files. Typical use interacts with many other classes, but there is no need to construct them as they are accessed using a property or method of their containing object.
- pptx.Presentation(pptx=None)[源代码]¶
Return a
Presentation
object loaded from pptx, where pptx can be either a path to a.pptx
file (a string) or a file-like object. If pptx is missing orNone
, the built-in default presentation “template” is loaded.
Presentation
objects¶
- class pptx.presentation.Presentation[源代码]¶
PresentationML (PML) presentation.
Not intended to be constructed directly. Use
pptx.Presentation()
to open or create a presentation.- property core_properties¶
Instance of
CoreProperties
holding the read/write Dublin Core document properties for this presentation.
- property notes_master¶
Instance of
NotesMaster
for this presentation. If the presentation does not have a notes master, one is created from a default template and returned. The same single instance is returned on each call.
- save(file)[源代码]¶
Save this presentation to file, where file can be either a path to a file (a string) or a file-like object.
- property slide_height¶
Height of slides in this presentation, in English Metric Units (EMU). Returns
None
if no slide width is defined. Read/write.
- property slide_layouts¶
Sequence of
SlideLayout
instances belonging to the firstSlideMaster
of this presentation. A presentation can have more than one slide master and each master will have its own set of layouts. This property is a convenience for the common case where the presentation has only a single slide master.
- property slide_master¶
First
SlideMaster
object belonging to this presentation. Typically, presentations have only a single slide master. This property provides simpler access in that common case.
- slide_masters[源代码]¶
Sequence of
SlideMaster
objects belonging to this presentation
- property slide_width¶
Width of slides in this presentation, in English Metric Units (EMU). Returns
None
if no slide width is defined. Read/write.
CoreProperties
objects¶
Each Presentation
object has a CoreProperties
object accessed via its
core_properties
attribute that provides read/write access to the
so-called core properties for the document. The core properties are author,
category, comments, content_status, created, identifier, keywords, language,
last_modified_by, last_printed, modified, revision, subject, title, and
version.
Each property is one of three types, str
, datetime.datetime
, or int
. String
properties are limited in length to 255 characters and return an empty string
(‘’) if not set. Date properties are assigned and returned as datetime.datetime
objects without timezone, i.e. in UTC. Any timezone conversions are the
responsibility of the client. Date properties return None
if not set.
python-pptx does not automatically set any of the document core properties other than to add a core properties part to a presentation that doesn’t have one (very uncommon). If python-pptx adds a core properties part, it contains default values for the title, last_modified_by, revision, and modified properties. Client code should change properties like revision and last_modified_by explicitly if that behavior is desired.
- class pptx.opc.coreprops.CoreProperties¶
- author¶
string – An entity primarily responsible for making the content of the resource.
- category¶
string – A categorization of the content of this package. Example values might include: Resume, Letter, Financial Forecast, Proposal, or Technical Presentation.
- comments¶
string – An account of the content of the resource.
- content_status¶
string – completion status of the document, e.g. ‘draft’
- created¶
datetime – time of intial creation of the document
- identifier¶
string – An unambiguous reference to the resource within a given context, e.g. ISBN.
- keywords¶
string – descriptive words or short phrases likely to be used as search terms for this document
- language¶
string – language the document is written in
- last_modified_by¶
string – name or other identifier (such as email address) of person who last modified the document
- last_printed¶
datetime – time the document was last printed
- modified¶
datetime – time the document was last modified
- revision¶
int – number of this revision, incremented by the PowerPoint® client once each time the document is saved. Note however that the revision number is not automatically incremented by python-pptx.
- subject¶
string – The topic of the content of the resource.
- title¶
string – The name given to the resource.
- version¶
string – free-form version string