Officeドキュメントの内部xml調査 #pptx編

2019年11月14日

テクノロジー プログラミング

この間、パスポートの更新をした際に、10年前の自分の写真と今の自分を比べて10年の老いを感じたのですが、自分でこれほど気がつくということは他人であれば、「老けたな〜〜」と言ってしまう気持ちもわからなくもないと、ひっそりと感じていた、ユゲタです。 この10年は仕事でもプライベートでも色々な躍進も変化もあり、あっという間でしたが、やはり10年って長い年月だと言うことを思い知らされました。 そんな話はどうでもいいのですが、最近やたらとOfficeドキュメントの調査をやっていて、ネットにあまり掲載されていないslideのxml情報を備忘録として載せておきます。 ちなみに、今回の記事は、pptxファイルの拡張子をzipに変更して解凍処理をした内部のファイル群についての調査になります。

解凍したファイル構成

pptxをzipに拡張子変更して回答したディレクトリ(pptx)の中身をfindコマンドで覗いてみました。 $ find ./pptx/ pptx/ pptx/[Content_Types].xml pptx/docProps pptx/docProps/app.xml pptx/docProps/thumbnail.jpeg pptx/docProps/core.xml pptx/_rels pptx/_rels/.rels pptx/ppt pptx/ppt/viewProps.xml pptx/ppt/slideLayouts pptx/ppt/slideLayouts/slideLayout9.xml pptx/ppt/slideLayouts/slideLayout8.xml pptx/ppt/slideLayouts/_rels pptx/ppt/slideLayouts/_rels/slideLayout5.xml.rels pptx/ppt/slideLayouts/_rels/slideLayout4.xml.rels pptx/ppt/slideLayouts/_rels/slideLayout2.xml.rels pptx/ppt/slideLayouts/_rels/slideLayout3.xml.rels pptx/ppt/slideLayouts/_rels/slideLayout8.xml.rels pptx/ppt/slideLayouts/_rels/slideLayout9.xml.rels pptx/ppt/slideLayouts/_rels/slideLayout11.xml.rels pptx/ppt/slideLayouts/_rels/slideLayout10.xml.rels pptx/ppt/slideLayouts/_rels/slideLayout6.xml.rels pptx/ppt/slideLayouts/_rels/slideLayout7.xml.rels pptx/ppt/slideLayouts/_rels/slideLayout1.xml.rels pptx/ppt/slideLayouts/slideLayout3.xml pptx/ppt/slideLayouts/slideLayout2.xml pptx/ppt/slideLayouts/slideLayout1.xml pptx/ppt/slideLayouts/slideLayout5.xml pptx/ppt/slideLayouts/slideLayout4.xml pptx/ppt/slideLayouts/slideLayout6.xml pptx/ppt/slideLayouts/slideLayout10.xml pptx/ppt/slideLayouts/slideLayout11.xml pptx/ppt/slideLayouts/slideLayout7.xml pptx/ppt/presentation.xml pptx/ppt/slideMasters pptx/ppt/slideMasters/slideMaster1.xml pptx/ppt/slideMasters/_rels pptx/ppt/slideMasters/_rels/slideMaster1.xml.rels pptx/ppt/slides pptx/ppt/slides/slide3.xml pptx/ppt/slides/slide2.xml pptx/ppt/slides/slide1.xml pptx/ppt/slides/_rels pptx/ppt/slides/_rels/slide2.xml.rels pptx/ppt/slides/_rels/slide3.xml.rels pptx/ppt/slides/_rels/slide1.xml.rels pptx/ppt/theme pptx/ppt/theme/theme1.xml pptx/ppt/_rels pptx/ppt/_rels/presentation.xml.rels pptx/ppt/revisionInfo.xml pptx/ppt/presProps.xml pptx/ppt/tableStyles.xml スライド3つのファイルですが、50個ぐらいのフォルダとファイルが確認できます。 少しまとめて、フォルダ構成だけみると以下のような感じです。 $ find pptx -type d pptx pptx/docProps pptx/_rels pptx/ppt pptx/ppt/slideLayouts pptx/ppt/slideLayouts/_rels pptx/ppt/slideMasters pptx/ppt/slideMasters/_rels pptx/ppt/slides pptx/ppt/slides/_rels pptx/ppt/theme pptx/ppt/_rels なんとなくどのディレクトリにどういう情報が入っているかはわかりやすいですね。 気になるのは、「_rels」というディレクトリですが、これは、その情報がファイルの色々な構成情報をもたせているファイルが格納されているようです。

slide情報

次に、実際のslide情報が格納されているファイルの中身で、テキスト操作するべき箇所を抜き出してみました。 pptxのデータ構成は、主に「テキスト」「画像」「テーブル(graphic)」で構成されているようで、その3つの記述を抜き出しています。

テキストデータ

<p:sp> <p:nvSpPr> <p:cNvPr id="3" name="Content Placeholder 2"/> <p:cNvSpPr> <a:spLocks noGrp="1"/> </p:cNvSpPr> <p:nvPr> <p:ph idx="1"/> </p:nvPr> </p:nvSpPr> <p:spPr/> <p:txBody> <a:bodyPr/> <a:lstStyle/> <a:p> <a:pPr marL="0" indent="0"> <a:buNone/> </a:pPr> <a:r> <a:rPr lang="en-US" dirty="0" smtClean="0"/> <a:t>Hoge-Hoge-Hoge-Hoge-Hoge !!!!!!!!</a:t> </a:r> <a:endParaRPr lang="en-US" dirty="0"/> </a:p> </p:txBody> </p:sp> 実際の表示されている部分は、下部の方にある"a:t"タグの内容がそれにあたるようです。 テキストが改行されている場合は、"a:p"タグが複数格納される仕様になっているようです。 wordpressの"p"タグの構成と似ていますね。

画像データ

<p:pic> <p:nvPicPr> <p:cNvPr id="5" name="Picture 4" descr="website-3407280_1280.jpg"/> <p:cNvPicPr> <a:picLocks noChangeAspect="1"/> </p:cNvPicPr> <p:nvPr/> </p:nvPicPr> <p:blipFill> <a:blip r:embed="rId3"> <a:extLst> <a:ext uri="{28A0092B-C50C-407E-A947-70E740481C1C}"> <a14:useLocalDpi xmlns:a14="http://schemas.microsoft.com/office/drawing/2010/main" val="0"/> </a:ext> </a:extLst> </a:blip> <a:stretch> <a:fillRect/> </a:stretch> </p:blipFill> <p:spPr> <a:xfrm> <a:off x="3994603" y="2438546"/> <a:ext cx="4192198" cy="2793707"/> </a:xfrm> <a:prstGeom prst="rect"> <a:avLst/> </a:prstGeom> </p:spPr> </p:pic> 画像データは、わかり易く"p:pic"タグに格納されています。 "descr"属性にファイル名が書かれていますが、このファイル名はたんなるdiscriptionなので、説明書きなので、ここのファイル名を変更しても、データには反映されません。 画像ファイルはrelsファイルに記述されてそこに書かれているid値をこのデータに格納する仕様になっています。 このデータでは、中盤にかかれている「r:embed="rId3"」の箇所がソレです。 ちなみに、このslide情報のrelsファイルは以下のようになっています。 <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"> <Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/slideLayout" Target="../slideLayouts/slideLayout2.xml"/> <Relationship Id="rId2" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/image" Target="../media/image1.png"/> <Relationship Id="rId3" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/image" Target="../media/image2.jpg"/> </Relationships> それほど難しい構成ではないですね。

Tableデータ

<p:graphicFrame> <p:nvGraphicFramePr> <p:cNvPr id="4" name="Table 3" title="convert-table"/> <p:cNvGraphicFramePr> <a:graphicFrameLocks noGrp="1"/> </p:cNvGraphicFramePr> <p:nvPr> <p:extLst> <p:ext uri="{D42A27DB-BD31-4B8C-83A1-F6EECF244321}"> <p14:modId xmlns:p14="http://schemas.microsoft.com/office/powerpoint/2010/main" val="2559022395"/> </p:ext> </p:extLst> </p:nvPr> </p:nvGraphicFramePr> <p:xfrm> <a:off x="567038" y="2716119"/> <a:ext cx="3419232" cy="1285240"/> </p:xfrm> <a:graphic> <a:graphicData uri="http://schemas.openxmlformats.org/drawingml/2006/table"> <a:tbl> <a:tblPr firstRow="1" bandRow="1"> <a:tableStyleId>{5C22544A-7EE6-4342-B048-85BDC9FD1C3A}</a:tableStyleId> </a:tblPr> <a:tblGrid> <a:gridCol w="1709616"> <a:extLst> <a:ext uri="{9D8B030D-6E8A-4147-A177-3AD203B41FA5}"> <a16:colId xmlns:a16="http://schemas.microsoft.com/office/drawing/2014/main" val="20000"/> </a:ext> </a:extLst> </a:gridCol> <a:gridCol w="1709616"> <a:extLst> <a:ext uri="{9D8B030D-6E8A-4147-A177-3AD203B41FA5}"> <a16:colId xmlns:a16="http://schemas.microsoft.com/office/drawing/2014/main" val="20001"/> </a:ext> </a:extLst> </a:gridCol> </a:tblGrid> <a:tr h="370840"> <a:tc> <a:txBody> <a:bodyPr/> <a:lstStyle/> <a:p> <a:r> <a:rPr lang="en-US"/> <a:t>1-a</a:t> </a:r> </a:p> </a:txBody> <a:tcPr/> </a:tc> <a:tc> <a:txBody> <a:bodyPr/> <a:lstStyle/> <a:p> <a:r> <a:rPr lang="en-US"/> <a:t>1-b</a:t> </a:r> </a:p> </a:txBody> <a:tcPr/> </a:tc> <a:extLst> <a:ext uri="{0D108BD9-81ED-4DB2-BD59-A6C34878D82A}"> <a16:rowId xmlns:a16="http://schemas.microsoft.com/office/drawing/2014/main" val="10000"/> </a:ext> </a:extLst> </a:tr> <a:tr h="370840"> <a:tc> <a:txBody> <a:bodyPr/> <a:lstStyle/> <a:p> <a:r> <a:rPr lang="en-US"/> <a:t>2-a</a:t> </a:r> </a:p> </a:txBody> <a:tcPr/> </a:tc> <a:tc> <a:txBody> <a:bodyPr/> <a:lstStyle/> <a:p> <a:r> <a:rPr lang="en-US"/> <a:t>2-b</a:t> </a:r> </a:p> <a:p> <a:r> <a:rPr lang="en-US"/> <a:t>---</a:t> </a:r> </a:p> <a:p> <a:endParaRPr lang="en-US"/> </a:p> </a:txBody> <a:tcPr/> </a:tc> <a:extLst> <a:ext uri="{0D108BD9-81ED-4DB2-BD59-A6C34878D82A}"> <a16:rowId xmlns:a16="http://schemas.microsoft.com/office/drawing/2014/main" val="10001"/> </a:ext> </a:extLst> </a:tr> </a:tbl> </a:graphicData> </a:graphic> </p:graphicFrame> tableデータはgraphicデータの構造で書かれています。 table構造事態は、htmlのtableタグと似ているので、xmlの内容をみているとなんとなく理解できるかと思います。 実際のtableタグは"a:tbl"タグがそれで、trタグは"<a:tr h="370840">"(※hは縦サイズ)。 "td"タグにあたるセル部分は"<a:tc>"でその内部は、テキスト構造と同じになっているようです。

構造さえ把握できれば怖くない

officeドキュメントって、パワーポイントのアプリを立ち上げて面倒くさい操作をするよりも、もしかするとxml操作をした方が確実にプレゼン資料が作れるという人もいるかもしれません。 こうした内部構造が理解できると、より効率的に作業が進められるし、今回この調査をしているのは、とあるコンバーターツールを作るためなのですが、今流行りのRPAというワケですね。 効率のいい作業をしようとすると、その内部構造に詳しくなれといういい参考になりました。

参考サイト

pptxファイルをテキストエディタで操作する参考になります。 .pptx ファイルと戯れる (主にテキストエディタで)

人気の投稿

このブログを検索

ごあいさつ

このWebサイトは、独自思考で我が道を行くユゲタの少し尖った思考のTechブログです。 毎日興味がどんどん切り替わるので、テーマはマルチになっています。 もしかしたらアイデアに困っている人の助けになるかもしれません。

ブログ アーカイブ