{"id":232,"date":"2021-01-10T07:46:09","date_gmt":"2021-01-09T23:46:09","guid":{"rendered":"http:\/\/blog.yuekegu.com\/?p=232"},"modified":"2021-01-10T07:46:09","modified_gmt":"2021-01-09T23:46:09","slug":"php%e8%af%bb%e5%8f%96word%e6%96%87%e6%a1%a3%e9%87%8c%e7%9a%84%e6%96%87%e5%ad%97%e5%8f%8a%e5%9b%be%e7%89%87%ef%bc%8c%e5%b9%b6%e4%bf%9d%e5%ad%98","status":"publish","type":"post","link":"https:\/\/book.yuekegu.com\/index.php\/2021\/01\/10\/php%e8%af%bb%e5%8f%96word%e6%96%87%e6%a1%a3%e9%87%8c%e7%9a%84%e6%96%87%e5%ad%97%e5%8f%8a%e5%9b%be%e7%89%87%ef%bc%8c%e5%b9%b6%e4%bf%9d%e5%ad%98\/","title":{"rendered":"PHP\u8bfb\u53d6word\u6587\u6863\u91cc\u7684\u6587\u5b57\u53ca\u56fe\u7247\uff0c\u5e76\u4fdd\u5b58"},"content":{"rendered":"\n<p>\u4e00\u3001composer\u5b89\u88c5phpWord<\/p>\n\n\n\n<p>composer require phpoffice\/phpword<\/p>\n\n\n\n<p>\u4f20\u9001\u95e8\uff1ahttps:\/\/packagist.org\/packages\/phpoffice\/phpword<\/p>\n\n\n\n<p>\u4e8c\u3001phpWord \u8bfb\u53d6 docx \u6587\u6863\uff08\u6ce8\u610f\u662fdocx\u683c\u5f0f\uff0cdoc\u683c\u5f0f\u4e0d\u884c\uff09<\/p>\n\n\n\n<p>\u5982\u679c\u4f60\u7684\u6587\u4ef6\u662fdoc\u683c\u5f0f\uff0c\u76f4\u63a5\u53e6\u5b58\u4e3a\u4e00\u4e2adocx\u5c31\u884c\u4e86\uff1b\u5982\u679c\u4f60\u7684doc\u6587\u6863\u8f83\u591a\uff0c\u53ef\u4ee5\u4e0b\u4e00\u4e2a\u6279\u91cf\u8f6c\u6362\u5de5\u5177\uff1ahttp:\/\/www.batchwork.com\/en\/doc2doc\/download.htm<\/p>\n\n\n\n<p>\u5982\u679c\u4f60\u8fd8\u6ca1\u914d\u7f6e\u81ea\u52a8\u52a0\u8f7d\uff0c\u5219\u5148\u914d\u7f6e\u4e00\u4e0b\uff1a<\/p>\n\n\n\n<p>require &#8216;.\/vendor\/autoload.php&#8217;;<\/p>\n\n\n\n<p>\u52a0\u8f7d\u6587\u6863\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$dir = str_replace('\\', '\/', <strong>DIR<\/strong>) . '\/';<br>\n$source = $dir . 'test.docx';<br>\n$phpWord = \\PhpOffice\\PhpWord\\IOFactory::load($source);<\/pre>\n\n\n\n<p>\u4e09\u3001\u5173\u952e\u70b9<\/p>\n\n\n\n<p>1\uff09\u5bf9\u9f50\u65b9\u5f0f\uff1aPhpOffice\\PhpWord\\Style\\Paragraph -&gt; getAlignment()<\/p>\n\n\n\n<p>2\uff09\u5b57\u4f53\u540d\u79f0\uff1a\\PhpOffice\\PhpWord\\Style\\Font -&gt; getName()<\/p>\n\n\n\n<p>3\uff09\u5b57\u4f53\u5927\u5c0f\uff1a\\PhpOffice\\PhpWord\\Style\\Font -&gt; getSize()<\/p>\n\n\n\n<p>4\uff09\u662f\u5426\u52a0\u7c97\uff1a\\PhpOffice\\PhpWord\\Style\\Font -&gt; isBold()<\/p>\n\n\n\n<p>5\uff09\u8bfb\u53d6\u56fe\u7247\uff1a\\PhpOffice\\PhpWord\\Element\\Image -&gt; getImageStringData()<\/p>\n\n\n\n<p>6\uff09ba64\u683c\u5f0f\u56fe\u7247\u6570\u636e\u4fdd\u5b58\u4e3a\u56fe\u7247\uff1afile_put_contents($imageSrc, base64_decode($imageData))<\/p>\n\n\n\n<p>\u56db\u3001\u5b8c\u6574\u4ee3\u7801<br> <\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"> require '.\/vendor\/autoload.php';\n function docx2html($source)\n {\n     $phpWord = \\PhpOffice\\PhpWord\\IOFactory::load($source);\n     $html = '';\n     foreach ($phpWord->getSections() as $section) {\n         foreach ($section->getElements() as $ele1) {\n             $paragraphStyle = $ele1->getParagraphStyle();\n             if ($paragraphStyle) {\n                 $html .= '\n             } else {\n                 $html .= '\n';             }             if ($ele1 instanceof \\PhpOffice\\PhpWord\\Element\\TextRun) {                 foreach ($ele1->getElements() as $ele2) {                     if ($ele2 instanceof \\PhpOffice\\PhpWord\\Element\\Text) {                         $style = $ele2->getFontStyle();                         $fontFamily = mb_convert_encoding($style->getName(), 'GBK', 'UTF-8');                         $fontSize = $style->getSize();                         $isBold = $style->isBold();                         $styleString = '';                         $fontFamily &amp;&amp; $styleString .= \"font-family:{$fontFamily};\";                         $fontSize &amp;&amp; $styleString .= \"font-size:{$fontSize}px;\";                         $isBold &amp;&amp; $styleString .= \"font-weight:bold;\";                         $html .= sprintf('\n';\n         }\n     }\n <code>return mb_convert_encoding($html, 'UTF-8', 'GBK');<\/code>\n }\n $dir = str_replace('\\', '\/', <strong>DIR<\/strong>) . '\/';\n $source = $dir . 'test.docx';\n echo docx2html($source);     <\/pre>\n\n\n\n<p>\u4e94\u3001\u8865\u5145<\/p>\n\n\n\n<p>\u5f88\u660e\u663e\uff0c\u8fd9\u662f\u4e00\u4e2a\u7b80\u964b\u7684word\u8bfb\u53d6\u793a\u4f8b\uff0c\u53ea\u8bfb\u53d6\u4e86\u6bb5\u843d\u7684\u5bf9\u9f50\u65b9\u5f0f\uff0c\u6587\u5b57\u7684\u5b57\u4f53\u3001\u5927\u5c0f\u3001\u662f\u5426\u52a0\u7c97\u53ca\u56fe\u7247\u7b49\u4fe1\u606f\uff0c\u5176\u4ed6\u4f8b\u5982\u6587\u5b57\u989c\u8272\u3001\u884c\u9ad8\u3002\u3002\u3002\u7b49\u7b49\u4fe1\u606f\u90fd\u5ffd\u60a0\u4e86\u3002\u9700\u8981\u7684\u8bdd\uff0c\u8bf7\u81ea\u884c\u67e5\u770bphpWord\u6e90\u7801\uff0c\u770b\\PhpOffice\\PhpWord\\Style\\xxx \u548c \\PhpOffice\\PhpWord\\Element\\xxx \u7b49\u7c7b\u91cc\u6709\u4ec0\u4e48\u8bfb\u53d6\u65b9\u6cd5\u5c31\u53ef\u4ee5\u4e86<\/p>\n\n\n\n<p>\u516d\u30012020-07-21 \u8865\u5145<\/p>\n\n\n\n<p>\u53ef\u4ee5\u7528\u4ee5\u4e0b\u65b9\u6cd5\u76f4\u63a5\u83b7\u53d6\u5230\u5b8c\u6574\u7684html<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$phpWord = \\PhpOffice\\PhpWord\\IOFactory::load('xxx.docx');<br>\n$xmlWriter = \\PhpOffice\\PhpWord\\IOFactory::createWriter($phpWord, \"HTML\");<br>\n$html = $xmlWriter-&gt;getContent();<\/pre>\n\n\n\n<p>\u6ce8\uff1ahtml\u5185\u5bb9\u91cc\u5305\u542b\u4e86head\u90e8\u5206\uff0c\u5982\u679c\u53ea\u9700\u8981style\u548cbody\u7684\u8bdd\uff0c\u9700\u8981\u81ea\u5df1\u5904\u7406\u4e00\u4e0b\uff1b\u7136\u540e\u56fe\u7247\u662fbase64\u7684\uff0c\u8981\u4fdd\u5b58\u7684\u8bdd\uff0c\u4e5f\u9700\u8981\u81ea\u5df1\u5904\u7406\u4e00\u4e0b<\/p>\n\n\n\n<p>base64\u6570\u636e\u4fdd\u5b58\u4e3a\u56fe\u7247\u8bf7\u53c2\u8003\u4e0a\u9762\u4ee3\u7801<\/p>\n\n\n\n<p>\u5982\u679c\u53ea\u60f3\u83b7\u53d6body\u91cc\u7684\u5185\u5bb9\uff0c\u53ef\u4ee5\u53c2\u8003 \\PhpOffice\\PhpWord\\Writer\\HTML\\Part\\Body \u91cc\u7684 write \u65b9\u6cd5<br>\n\u590d\u5236\u4ee3\u7801<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$phpWord = \\PhpOffice\\PhpWord\\IOFactory::load('xxxx.docx');<br>\n$htmlWriter = \\PhpOffice\\PhpWord\\IOFactory::createWriter($phpWord, \"HTML\");<br>\n$content = '';<br>\nforeach ($phpWord-&gt;getSections() as $section) {<br>\n    $writer = new \\PhpOffice\\PhpWord\\Writer\\HTML\\Element\\Container($htmlWriter, $section);<br>\n    $content .= $writer-&gt;write();<br>\n}<br>\necho $content;exit;<\/pre>\n\n\n\n<p>\u56fe\u7247\u7684\u5904\u7406\u7684\u8bdd\uff0c\u6682\u65f6\u6ca1\u6709\u597d\u529e\u6cd5\u80fd\u5728\u4e0d\u4fee\u6539\u6e90\u7801\u7684\u60c5\u51b5\u4e0b\u5904\u7406\u597d\uff0c\u6539\u6e90\u7801\u7684\u8bdd\uff0c\u76f8\u5173\u4ee3\u7801\u5728 \\PhpOffice\\PhpWord\\Writer\\HTML\\Element\\Image \u91cc<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">public function write()\n {\n     if (!$this->element instanceof ImageElement) {\n         return '';\n     }\n     $content = '';\n     $imageData = $this->element->getImageStringData(true);\n     if ($imageData !== null) {\n         $styleWriter = new ImageStyleWriter($this->element->getStyle());\n         $style = $styleWriter->write();\n         \/\/ $imageData = 'data:' . $this->element->getImageType() . ';base64,' . $imageData;\n         $imageSrc = 'images\/' . md5($this->element->getSource()) . '.' . $this->element->getImageExtension();\n         \/\/ \u8fd9\u91cc\u53ef\u4ee5\u81ea\u5df1\u5904\u7406\uff0c\u4e0a\u4f20oss\u4e4b\u7c7b\u7684\n         file_put_contents($imageSrc, base64_decode($imageData));\n <code>    $content .= $this->writeOpening();     $content .= \"&lt;img border=\\\"0\\\" style=\\\"{$style}\\\" src=\\\"{$imageSrc}\\\"\/>\";     $content .= $this->writeClosing(); } return $content;<\/code>\n }<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u3001composer\u5b89\u88c5phpWord composer require phpoffice\/phpword \u4f20\u9001\u95e8\uff1ahttps:\/\/packagist.org\/packages\/phpoffice\/phpword \u4e8c\u3001phpWord \u8bfb\u53d6 docx \u6587\u6863\uff08\u6ce8\u610f\u662fdocx\u683c\u5f0f\uff0cdoc\u683c\u5f0f\u4e0d\u884c\uff09 \u5982\u679c\u4f60\u7684\u6587\u4ef6\u662fdoc\u683c\u5f0f\uff0c\u76f4\u63a5\u53e6\u5b58\u4e3a\u4e00\u4e2adocx\u5c31\u884c\u4e86\uff1b\u5982\u679c\u4f60\u7684doc\u6587\u6863\u8f83\u591a\uff0c\u53ef\u4ee5\u4e0b\u4e00\u4e2a\u6279\u91cf\u8f6c\u6362\u5de5\u5177\uff1ahttp:\/\/www.batchwork.com\/en\/doc2doc\/download.htm \u5982\u679c\u4f60\u8fd8\u6ca1\u914d\u7f6e\u81ea\u52a8\u52a0\u8f7d\uff0c\u5219\u5148\u914d\u7f6e\u4e00\u4e0b\uff1a require &#8216;.\/vendor\/autoload.php&#8217;; \u52a0\u8f7d\u6587\u6863\uff1a $dir = str_replace(&#8216;\\&#8217;, &#8216;\/&#8217;, DIR) . &#8216;\/&#8217;; $source = $dir . &#8216;test.docx&#8217;; $phpWord = \\PhpOffice\\PhpWord\\IOFactory::load($source); \u4e09\u3001\u5173\u952e\u70b9 1\uff09\u5bf9\u9f50\u65b9\u5f0f\uff1aPhpOffice\\PhpWord\\Style\\Paragraph -&gt; getAlignment() 2\uff09\u5b57\u4f53\u540d\u79f0\uff1a\\PhpOffice\\PhpWord\\Style\\Font -&gt; getName() 3\uff09\u5b57\u4f53\u5927\u5c0f\uff1a\\PhpOffice\\PhpWord\\Style\\Font -&gt; getSize() 4\uff09\u662f\u5426\u52a0\u7c97\uff1a\\PhpOffice\\PhpWord\\Style\\Font -&gt; isBold() [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[27],"tags":[],"class_list":["post-232","post","type-post","status-publish","format-standard","hentry","category-27"],"_links":{"self":[{"href":"https:\/\/book.yuekegu.com\/index.php\/wp-json\/wp\/v2\/posts\/232","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/book.yuekegu.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/book.yuekegu.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/book.yuekegu.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/book.yuekegu.com\/index.php\/wp-json\/wp\/v2\/comments?post=232"}],"version-history":[{"count":0,"href":"https:\/\/book.yuekegu.com\/index.php\/wp-json\/wp\/v2\/posts\/232\/revisions"}],"wp:attachment":[{"href":"https:\/\/book.yuekegu.com\/index.php\/wp-json\/wp\/v2\/media?parent=232"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/book.yuekegu.com\/index.php\/wp-json\/wp\/v2\/categories?post=232"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/book.yuekegu.com\/index.php\/wp-json\/wp\/v2\/tags?post=232"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}