
    MiT              
      @   S r SSKJr  SSKrSSKJr  SSKJr  SSKJ	r	J
r
JrJrJr  SSKrSSKJr  SSKJr  SS	KJr  SS
KJr  SSKJr  SSKJr  SSKJr  SSKJr  SSKJ r   SSK!J"r"  SSK#J$r$J%r%  SSK&J'r'J(r(J)r)J*r*J+r+J,r,J-r-J.r.J/r/  SSK0J1r1  SSK2J3r3J4r4  SSK5J6r6J7r7J8r8  SSK9J:r:  SSK;J<r<  Sr=S&S jr> " S S\5      r?\3" \1R                  5      \" S'SSSSS\:R                  S.                 S(S jjj5       5       rB " S  S!5      rC " S" S#5      rD " S$ S%5      rEg))zPartitioner for PPTX documents.

PPTX files are PowerPoint 2007+ documents. These are XML-based and "open" (documented ISO standard),
unlike the `.ppt` format which was binary and proprietary.
    )annotationsN)cached_property)SpooledTemporaryFile)IOAnyIteratorProtocolSequence)Presentation)Shape)	BaseShape)GraphicFrame)
GroupShape)Picture)_BaseGroupShapes)Slide)
_Paragraph)add_chunking_strategy)	HtmlTablehtmlify_matrix_of_cell_texts)	ElementElementMetadataEmailAddressListItemNarrativeText	PageBreakTableTextTitle)FileType)apply_metadataget_last_modified_date)is_email_addressis_possible_narrative_textis_possible_title)PartitionStrategy)is_temp_file_pathpptxAbstractPicturePartitionerc                .    [         R                  U 5        gzLSpecify a pluggable sub-partitioner to be used for partitioning PPTX images.N)PptxPartitionerOptionsregister_picture_partitioner)picture_partitioners    u/var/www/eduai.edurigo.com/storigo/production/storigo_env/lib/python3.13/site-packages/unstructured/partition/pptx.pyr-   r-   3   s    778KL    c                  ,    \ rS rSrSr\SS j5       rSrg)r)   =   a  Defines the interface for a pluggable sub-partitioner for PPTX Picture objects.

A PPTX Picture object generally contains an image (e.g. JPG, PNG) but can also contain other
media types like a video or sound file. The interface classmethod generates zero-or-more
elements from the specified Picture object. If the media in the picture object is not supported
then it will silently return without generating any elements.
c                    g)zHGenerate document elements derived from `picture`, a PPTX Picture shape.N clspictureoptss      r/   iter_elements(AbstractPicturePartitioner.iter_elementsF   s     	r0   r4   Nr7   r   r8   r,   returnIterator[Element]__name__
__module____qualname____firstlineno____doc__classmethodr9   __static_attributes__r4   r0   r/   r)   r)   =   s      r0   T   )fileinclude_page_breaksinclude_slide_notesinfer_table_structurestarting_page_numberstrategyc          
     \    [        UU UUUUUS9n[        [        R                  U5      5      $ )a  Partition PowerPoint document in .pptx format into its document elements.

Parameters
----------
filename
    A string defining the target filename path.
file
    A file-like object using "rb" mode --> open(filename, "rb").
include_page_breaks
    If True, includes a PageBreak element between slides
include_slide_notes
    If True, includes the slide notes as element
infer_table_structure
    If True, any Table elements that are extracted will also have a metadata field
    named "text_as_html" where the table's text content is rendered into an html string.
    I.e., rows and cells are preserved.
    Whether True or False, the "text" field is always present in any Table element
    and is the text content of the table (no structure).
starting_page_number
    Indicates what page number should be assigned to the first slide in the presentation.
    This information will be reflected in elements' metadata and can be be especially
    useful when partitioning a document that is part of a larger document.
)rG   	file_pathrH   rI   rJ   rL   rK   )r,   list_PptxPartitioneriter_presentation_elements)	filenamerG   rH   rI   rJ   rK   rL   kwargsr8   s	            r/   partition_pptxrT   Q   s=    H "//31D  ;;DABBr0   c                      \ rS rSrSrSS jr\SS j5       rSS jrSS jr	SS jr
SS jrSS	 jrSS
 jrSS jrSS jr\SS j5       rSS jrSrg)rP      z;Provides `.partition()` for PowerPoint 2007+ (.pptx) files.c                    Xl         g N_opts)selfr8   s     r/   __init___PptxPartitioner.__init__   s    
r0   c                .    U " U5      R                  5       $ )zFPartition MS Word documents (.docx format) into its document elements.)_iter_presentation_elements)r6   r8   s     r/   rQ   +_PptxPartitioner.iter_presentation_elements   s     4y4466r0   c              #    #    U R                   R                   GH1  nU R                  R                  5        Sh  vN   U R	                  U5       Sh  vN   U R                  U5      u  p#U H  nUR                  (       a2  [        U[        5      (       d   eU R                  U5       Sh  vN   MF  UR                  (       aR  [        U[        5      (       d   eXB:X  a  U R                  U5       Sh  vN   M  U R                  U5       Sh  vN   M  [        U[        5      (       d  M  U R                  U5       Sh  vN   M     GM4     g GN N N Nb NI N7f)zAGenerate each document-element in presentation in document order.N)_presentationslidesrZ   increment_page_number_iter_maybe_slide_notes_order_shapes	has_table
isinstancer   _iter_table_elementhas_text_framer   _iter_title_shape_element_iter_shape_elementsr   _iter_picture_elements)r[   slidetitle_shapeshapesshapes        r/   r_   ,_PptxPartitioner._iter_presentation_elements   s     ''..Ezz7799933E:::"&"4"4U";K??%e\::::#77>>>))%eU3333+#'#A#A%#HHH#'#<#<U#CCCw//#::5AAA   /9: ? ICAsq   8EEEEAE,E-AE4E5EEE-EEEEEEEEc                J    [        UR                  R                  S5      5      $ )zxTrue when `paragraph` has a bullet-charcter prefix.

Bullet characters in the openxml schema are represented by buChar.
z./a:pPr/a:buChar)bool_pxpath)r[   	paragraphs     r/   _is_bulleted_paragraph'_PptxPartitioner._is_bulleted_paragraph   s     ILL&&'9:;;r0   c              #  <  #    U R                   R                  (       d  gUR                  (       d  gUR                  nUR                  nU(       d  gUR
                  R                  5       nU(       d  g[        X@R                   R                  5       [        S9v   g7f)z?Generate zero-or-one NarrativeText element for the slide-notes.Ntextmetadatadetection_origin)
rZ   rI   has_notes_slidenotes_slidenotes_text_framer|   stripr   text_metadataDETECTION_ORIGIN)r[   rn   r   r   
notes_texts        r/   re   (_PptxPartitioner._iter_maybe_slide_notes   s      zz-- $$''&77  %**002
 jj&>&>&@Sc
 	
s   BBc              #     #    U R                   R                  nUR                  XR                   5       Sh  vN   g N7f)z6Generate elements derived from the image in `picture`.N)rZ   r.   r9   )r[   r7   PicturePartitionerClss      r/   rm   '_PptxPartitioner._iter_picture_elements   s/      !%

 > >(66w

KKKs   5?=?c              #    #    U R                  U5      (       a  gUR                  R                   GH  nUR                  nUR	                  5       S:X  a  M&  UR
                  =(       d    SnU R                  R                  US9nU R                  U5      (       a  [        X5[        S9v   M|  [        U5      (       a  [        U[        S9v   M  [        U5      (       a  [        UU[        S9v   M  [        U5      (       a.  U R                  R                  US-   S9n[!        X5[        S9v   M  [#        X5[        S9v   GM     g7f)z?Generate Text or subtype element for each paragraph in `shape`.N r   category_depthr{   r|   r~   rF   )_shape_is_off_slide
text_frame
paragraphsr|   r   levelrZ   r   rx   r   r   r#   r   r$   r   r%   r   r   )r[   rq   rw   r|   r   r}   s         r/   rl   %_PptxPartitioner._iter_shape_elements   s    ##E**))44I>>Dzz|r!OO(qEzz//u/EH**955DN^__!$''"?OPP+D11#%%5 
 #4((::335193MK[\\JZ[[/ 5s   EEc           
   #    #    [        UR                  R                  5      =n(       d  g[        U VVs/ s H(  o3R                   Vs/ s H  oDR
                  PM     snPM*     snn5      n[        R                  " U5      nUR
                  (       d  gU R                  R                  U R                  R                  (       a  UR                  OS5      n[        UR
                  U[        S9v   gs  snf s  snnf 7f)zjGenerate zero-or-one Table element for the table in `shape`.

An empty table does not produce an element.
Nr{   )rO   tablerowsr   cellsr|   r   from_html_textrZ   table_metadatarJ   htmlr   r   )r[   graphfrmr   rowcell	html_text
html_tabler}   s           r/   ri   $_PptxPartitioner._iter_table_element   s     
 X^^001110:>?$3II.IDiiI.$?
	 --i8
::,,#zz??JOOT
 8N^__ /?s)   2C<C6
C1C6
#BC<1C6
6C<c              #    #    U R                  U5      (       a  gSnUR                  R                   H  nUR                  nUR	                  5       S:X  a  M%  U R                  U5      (       a?  UR                  =(       d    Sn[        UU R                  R                  US9[        S9v   Mz  [        U5      (       a  [        U[        S9v   M  [        UU R                  R                  US9[        S9v   US-  nM     g7f)zGenerate Title element for each paragraph in title `shape`.

Text is most likely a title, but in the rare case that the title shape was used
for the slide body text, also check for bulleted paragraphs.Nr   r   r   r{   r   rF   )r   r   r   r|   r   rx   r   r   rZ   r   r   r#   r   r   )r[   rq   depthrw   r|   bullet_depths         r/   rk   *_PptxPartitioner._iter_title_shape_element  s     
 ##E**))44I>>Dzz|r!**955(3!!ZZ55\5R%5 
 "$''"?OPP !ZZ55U5K%5 
 
+ 5s   C>D c                ~   ^ SU4S jjmSS jnUR                   R                  [        T" UR                   5      US94$ )zOrders the shapes on `slide` from top to bottom and left to right.

Returns the title shape if it exists and the ordered shapes.c              3     >#    U  H8  n[        U[        5      (       a  T" UR                  5       S h  vN   M4  Uv   M:     g  N7frX   )rh   r   rp   )rp   rq   iter_shapess     r/   r   3_PptxPartitioner._order_shapes.<locals>.iter_shapes2  s7     eZ00*5<<888K	  8s   0AAAc                V    U R                   =(       d    SU R                  =(       d    S4$ Nr   )topleft)rq   s    r/   sort_key0_PptxPartitioner._order_shapes.<locals>.sort_key9  s    99>5::?22r0   )key)rp   r   r<   zIterator[BaseShape])rq   r   r<   ztuple[int, int])rp   titlesorted)r[   rn   r   r   s      @r/   rf   _PptxPartitioner._order_shapes-  s4    
	 	3 ||!!6+ell*C#RRRr0   c                V    [         R                  " U R                  R                  5      $ )zKThe python-pptx `Presentation` object loaded from the provided source file.)r(   r   rZ   	pptx_filer[   s    r/   rb   _PptxPartitioner._presentation>  s       !5!566r0   c                    [        UR                  =(       a    UR                  =(       a%    UR                  S:  =(       d    UR                  S:  5      $ r   )rt   r   r   )r[   rq   s     r/   r   $_PptxPartitioner._shape_is_off_slideC  s7     UYY-5::TEIIM4SUZZRS^UUr0   rY   N)r8   r,   )r8   r,   r<   r=   )r<   r=   )rw   r   r<   rt   )rn   r   r<   zIterator[NarrativeText])r7   r   r<   r=   )rq   r   r<   r=   )r   r   r<   zIterator[Table])rn   r   r<   z(tuple[Shape | None, Sequence[BaseShape]])r<   r   )rq   r   r<   rt   )r?   r@   rA   rB   rC   r\   rD   rQ   r_   rx   re   rm   rl   ri   rk   rf   r   rb   r   rE   r4   r0   r/   rP   rP      sg    E 7 7BH<
4L\<`,@S" 7 7Vr0   rP   c                  4   \ rS rSrSrSr SS.             SS jjr\SS j5       r\	SS j5       r
\	SS	 j5       rSS
 jr\	SS j5       r\	SS j5       r\	SS j5       r\SS j5       r\	SS j5       r\	SS j5       r\	SS j5       rSS jrSS S jjrSrg)!r,   iI  zVEncapsulates partitioning option validation, computation, and application of defaults.NrF   )rK   c               `    Xl         X l        X0l        X@l        XPl        X`l        US-
  U l        g )NrF   )_file
_file_path_include_page_breaks_include_slide_notes_infer_table_structure	_strategy_page_counter)r[   rG   rN   rH   rI   rJ   rL   rK   s           r/   r\   PptxPartitionerOptions.__init__V  s2     
#$7!$7!&;#!1A5r0   c                    Xl         gr+   )_PicturePartitionerCls)r6   r.   s     r/   r-   3PptxPartitionerOptions.register_picture_partitionerj  s
     &9"r0   c                    U R                   $ )a'  When True, include `PageBreak` elements in element-stream.

Note that regardless of this setting, page-breaks are detected, and page-number is tracked
and included in element metadata. Only the presence of distinct `PageBreak` elements (which
contain no text) in the element stream is affected.
)r   r   s    r/   rH   *PptxPartitionerOptions.include_page_breakso  s     (((r0   c                8    U R                   c  S$ U R                   $ )zNWhen True, also partition any text found in slide notes as part of each slide.F)r   r   s    r/   rI   *PptxPartitionerOptions.include_slide_notesy  s      119uXt?X?XXr0   c           	   #     #    U =R                   S-  sl         U R                   S:  a  gU R                  (       a1  [        S[        [	        U R
                  U R                  S-
  S9S9v   gg7f)zGIncrement page-number by 1 and generate a PageBreak element if enabled.rF      Nr   )last_modifiedpage_number)r~   r}   )r   r   r   r   r   r   r   r   s    r/   rd   ,PptxPartitionerOptions.increment_page_number~  sg     a!$$!1("&"4"4$BRBRUVBV  %s   A*A,c                    U R                   $ )zRTrue when partitioner should compute and apply `text_as_html` metadata for tables.)r   r   s    r/   rJ   ,PptxPartitionerOptions.infer_table_structure  s     ***r0   c                    U R                   (       d  g[        U R                   5      (       a  S$ [        U R                   5      $ )zHThe best last-modified date available, None if no sources are available.N)r   r'   r"   r   s    r/   r   $PptxPartitionerOptions.last_modified  s:      &doo66D	
<RSWSbSb<c	
r0   c                    U R                   $ )zHThe best available file-path for this document or `None` if unavailable.)r   r   s    r/   metadata_file_path)PptxPartitionerOptions.metadata_file_path  s     r0   c                    U R                   $ )z The current page (slide) number.)r   r   s    r/   r   "PptxPartitionerOptions.page_number  s     !!!r0   c                @    U R                   c  [        $ U R                   $ )z3The sub-partitioner to use for PPTX Picture shapes.)r   _NullPicturePartitionerr   s    r/   r.   *PptxPartitionerOptions.picture_partitioner  s*     **2 $	
 ,,	
r0   c                \   U R                   (       a  U R                   $ [        U R                  [        5      (       aI  U R                  R	                  S5        [
        R                  " U R                  R                  5       5      $ U R                  (       a  U R                  $ [        S5      e)zThe PowerPoint document file to be partitioned.

This is either a str path or a file-like object. `python-pptx` accepts either for opening a
presentation file.
r   zQNo PPTX document specified, either `filename` or `file` argument must be provided)	r   rh   r   r   seekioBytesIOread
ValueErrorr   s    r/   r    PptxPartitionerOptions.pptx_file  st     ????"
 djj"677JJOOA::djjoo/00::::_
 	
r0   c                    U R                   $ )a  The requested partitioning strategy.

This indicates whether the partitioner should undertake expensive operations like inference
and OCR to produce a more thorough and/or accurate partitioning of the document.

Can take several values but for PPTX purposes there is only "hi_res" and not "hi_res".
Depending on the picture-partitioner used, images may only be OCR'ed and added to the
element-stream when this partitioning strategy is "hi_res".
)r   r   s    r/   rL   PptxPartitionerOptions.strategy  s     ~~r0   c                p    [        U R                  U R                  U R                  US9n[        Ul        U$ )z=ElementMetadata instance suitable for use with Table element.)rR   r   r   text_as_htmlr   r   r   r   r   r~   )r[   r   element_metadatas      r/   r   %PptxPartitionerOptions.table_metadata  s<    *,,,,((%	
 -=)r0   c                p    [        U R                  U R                  U R                  US9n[        Ul        U$ )zAElementMetadata instance suitable for use with Text and subtypes.)rR   r   r   r   r   )r[   r   r   s      r/   r   $PptxPartitionerOptions.text_metadata  s<    *,,,,(()	
 -=)r0   )r   r   r   r   r   r   r   )rG   IO[bytes] | NonerN   
str | NonerH   rt   rI   bool | NonerJ   rt   rL   strrK   int)r.   r)   )r<   rt   )r<   zIterator[PageBreak])r<   r   )r<   r   )r<   r)   )r<   zstr | IO[bytes])r<   r   )r   r   )r   )r   r   r<   r   )r?   r@   rA   rB   rC   r   r\   rD   r-   r   rH   rI   rd   rJ   r   r   propertyr   r.   r   rL   r   r   rE   r4   r0   r/   r,   r,   I  s>   `!$ %&6 6 	6
 "6 )6  $6 6 "6( 9 9 ) ) Y Y  + + 
 
   " " 
 
  
 
. 
 
	 	  	 r0   r,   c                  ,    \ rS rSrSr\SS j5       rSrg)r   i  zHDoes not parse the provided Picture element and generates zero elements.c              #     #    g7f)zNo-op picture partitioner.Nr4   r5   s      r/   r9   %_NullPicturePartitioner.iter_elements  s
      	s   r4   Nr;   r>   r4   r0   r/   r   r     s    R r0   r   )r.   r)   r<   NonerX   )rR   r   rG   r   rH   rt   rI   r   rJ   rt   rK   r   rL   r   rS   r   r<   zlist[Element])FrC   
__future__r   r   	functoolsr   tempfiler   typingr   r   r   r	   r
   r(   pptx.presentationr   pptx.shapes.autoshaper   pptx.shapes.baser   pptx.shapes.graphfrmr   pptx.shapes.groupr   pptx.shapes.picturer   pptx.shapes.shapetreer   
pptx.slider   pptx.text.textr   unstructured.chunkingr   unstructured.common.html_tabler   r   unstructured.documents.elementsr   r   r   r   r   r   r   r   r   unstructured.file_utils.modelr    &unstructured.partition.common.metadatar!   r"    unstructured.partition.text_typer#   r$   r%   &unstructured.partition.utils.constantsr&   unstructured.utilsr'   r   r-   r)   PPTXFASTrT   rP   r,   r   r4   r0   r/   <module>r     sV   # 	 % ) 8 8  * ' & - ( ' 2  % 7 R
 
 
 3 Y 
 E 0 M ( ,C " $'+"& !%**,C,C ,C 	,C
 %,C  ,C ,C ,C ,C ,C  ,C^DV DVNh  h ` r0   