Open word file with php

How we open a ms word document file with php?

It depends on what you want to do. You can pass the request to the system (only works for specific systems) or you can perhaps find a library that will allow you to do it.

AFAIK, only text files can be opened without special manipulation in most programming languages. …

It depends on what you want to do. You can pass the request to the system (only works for specific systems) or you can perhaps find a library that will allow you to do it.

AFAIK, only text files can be opened without special manipulation in most programming languages. (Database …

Puckdropper is right, none of these code samples will actually work, as MS Word uses a binary file format, and might even refuse to open a file that had been modified with random bbcode/html added on the end. If you try very basic examples it will work, however MS Word …

All 13 Replies

It depends on what you want to do. You can pass the request to the system (only works for specific systems) or you can perhaps find a library that will allow you to do it. AFAIK, only text files can be opened without special manipulation in most programming languages. (Database access does not count.)

It depends on what you want to do. You can pass the request to the system (only works for specific systems) or you can perhaps find a library that will allow you to do it. AFAIK, only text files can be opened without special manipulation in most programming languages. (Database access does not count.)

you can open an ms word document with its coordinations,How?OK Look:
to make a coordination you must use this:
$opened_file=file(«THE FILE PATH»)
$old_array=array(‘‘,»,’‘);
$new_array=array(»,»,»);
$string=str_replace($old_array,$new_array,$opened_file);
To open a file use this:

Puckdropper is right, none of these code samples will actually work, as MS Word uses a binary file format, and might even refuse to open a file that had been modified with random bbcode/html added on the end. If you try very basic examples it will work, however MS Word creates all sorts of problems in the background using Word 97-2003 files. .docx files will fail entirely because of the zip file format. What you really need to do is use COM to tell MS Word itself to handle parsing the file. You can create a new COM object with $word = new COM(«word.application»); which basically opens up a copy of MS Word on the server and gives you access to it from PHP. Take a look at this blog post on opening microsoft word files in php and extracting text. Naturally you need a Windows based server in order to do this.

Источник

Читайте также:  Можно ли переопределить статический метод java

Saved searches

Use saved searches to filter your results more quickly

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

A pure PHP library for reading and writing word processing documents

License

Unknown and 2 other licenses found

Licenses found

PHPOffice/PHPWord

This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

Sign In Required

Please sign in to use Codespaces.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching Xcode

If nothing happens, download Xcode and try again.

Launching Visual Studio Code

Your codespace will open once ready.

There was a problem preparing your codespace, please try again.

Latest commit

Git stats

Files

Failed to load latest commit information.

README.md

Latest Stable Version Code Quality Code Coverage Total Downloads License

PHPWord is a library written in pure PHP that provides a set of classes to write to and read from different document file formats. The current version of PHPWord supports Microsoft Office Open XML (OOXML or OpenXML), OASIS Open Document Format for Office Applications (OpenDocument or ODF), Rich Text Format (RTF), HTML, and PDF.

PHPWord is an open source project licensed under the terms of LGPL version 3. PHPWord is aimed to be a high quality software product by incorporating continuous integration and unit testing. You can learn more about PHPWord by reading the Developers’ Documentation.

If you have any questions, please ask on StackOverFlow

With PHPWord, you can create OOXML, ODF, or RTF documents dynamically using your PHP scripts. Below are some of the things that you can do with PHPWord library:

  • Set document properties, e.g. title, subject, and creator.
  • Create document sections with different settings, e.g. portrait/landscape, page size, and page numbering
  • Create header and footer for each sections
  • Set default font type, font size, and paragraph style
  • Use UTF-8 and East Asia fonts/characters
  • Define custom font styles (e.g. bold, italic, color) and paragraph styles (e.g. centered, multicolumns, spacing) either as named style or inline in text
  • Insert paragraphs, either as a simple text or complex one (a text run) that contains other elements
  • Insert titles (headers) and table of contents
  • Insert text breaks and page breaks
  • Insert and format images, either local, remote, or as page watermarks
  • Insert binary OLE Objects such as Excel or Visio
  • Insert and format table with customized properties for each rows (e.g. repeat as header row) and cells (e.g. background color, rowspan, colspan)
  • Insert list items as bulleted, numbered, or multilevel
  • Insert hyperlinks
  • Insert footnotes and endnotes
  • Insert drawing shapes (arc, curve, line, polyline, rect, oval)
  • Insert charts (pie, doughnut, bar, line, area, scatter, radar)
  • Insert form fields (textinput, checkbox, and dropdown)
  • Create document from templates
  • Use XSL 1.0 style sheets to transform headers, main document part, and footers of an OOXML template
  • . and many more features on progress
Читайте также:  How to convert value to string in java

PHPWord requires the following:

  • PHP 7.1+
  • XML Parser extension
  • Laminas Escaper component
  • Zip extension (optional, used to write OOXML and ODF)
  • GD extension (optional, used to add images)
  • XMLWriter extension (optional, used to write OOXML and ODF)
  • XSL extension (optional, used to apply XSL style sheet to template )
  • dompdf library (optional, used to write PDF)

PHPWord is installed via Composer. To add a dependency to PHPWord in your project, either

Run the following to use the latest stable version

composer require phpoffice/phpword

or if you want the latest unreleased version

composer require phpoffice/phpword:dev-master

The following is a basic usage example of the PHPWord library.

 require_once 'bootstrap.php'; // Creating the new document. $phpWord = new \PhpOffice\PhpWord\PhpWord(); /* Note: any element you append to a document must reside inside of a Section. */ // Adding an empty Section to the document. $section = $phpWord->addSection(); // Adding Text element to the Section having font styled by default. $section->addText( '"Learn from yesterday, live for today, hope for tomorrow. ' . 'The important thing is not to stop questioning." ' . '(Albert Einstein)' ); /* * Note: it's possible to customize font style of the Text element you add in three ways: * - inline; * - using named font style (new font style object will be implicitly created); * - using explicitly created font style object. */ // Adding Text element with font customized inline. $section->addText( '"Great achievement is usually born of great sacrifice, ' . 'and is never the result of selfishness." ' . '(Napoleon Hill)', array('name' => 'Tahoma', 'size' => 10) ); // Adding Text element with font customized using named font style. $fontStyleName = 'oneUserDefinedStyle'; $phpWord->addFontStyle( $fontStyleName, array('name' => 'Tahoma', 'size' => 10, 'color' => '1B2232', 'bold' => true) ); $section->addText( '"The greatest accomplishment is not in never falling, ' . 'but in rising again after you fall." ' . '(Vince Lombardi)', $fontStyleName ); // Adding Text element with font customized using explicitly created font style object. $fontStyle = new \PhpOffice\PhpWord\Style\Font(); $fontStyle->setBold(true); $fontStyle->setName('Tahoma'); $fontStyle->setSize(13); $myTextElement = $section->addText('"Believe you can and you\'re halfway there." (Theodor Roosevelt)'); $myTextElement->setFontStyle($fontStyle); // Saving the document as OOXML file. $objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007'); $objWriter->save('helloWorld.docx'); // Saving the document as ODF file. $objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'ODText'); $objWriter->save('helloWorld.odt'); // Saving the document as HTML file. $objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'HTML'); $objWriter->save('helloWorld.html'); /* Note: we skip RTF, because it's not XML-based and requires a different example. */ /* Note: we skip PDF, because "HTML-to-PDF" approach is used to create PDF documents. */

More examples are provided in the samples folder. For an easy access to those samples launch php -S localhost:8000 in the samples directory then browse to http://localhost:8000 to view the samples. You can also read the Developers’ Documentation for more detail.

We welcome everyone to contribute to PHPWord. Below are some of the things that you can do to contribute.

  • Read our contributing guide.
  • Fork us and request a pull to the master branch.
  • Submit bug reports or feature requests to GitHub.
  • Follow @PHPWord and @PHPOffice on Twitter.

Источник

Устройство формата DOCX, как его открыть на PHP для вывода данных

10.07.18 ИТ / PHP 19734

Сайт сегодня – это непросто странички, а в большинстве случаев целая система по обработке и генерации различных данных. Часто бывает необходимо вывести данные с сайта в какой-либо распространенный формат, например, в PDF, DOCX, CSV и т.д. Рассмотрим, как можно вывести данные в файл DOCX на PHP.

Немного о формате DOCX. Это расширение файлов для программы Microsoft Office, продукт Word. Microsoft Word представляет собой текстовый процессор, который предназначен для создания, просмотра и редактирования текстовых документов. Раньше Word в основном использовался двоичный формат сохранения файлов, расширение DOC. Но, на смену ему пришел более совершенный формат – DOCX. Как устроен DOCX?

DOCX – это просто архив, в котором содержатся все необходимые файлы для документа, сам документ хранится в формате XML. Это делает возможным легко открыть архив на PHP и вывести нужные данные в файл XML внутри контейнера DOCX. Чтобы лучше понять, что содержится в таком файле, можно взять любой архиватор и открыть с его помощью файл DOCX. Внутри Вы увидите определенную структуру из папок и файлов.

docx-structure

Основной интерес представляет собой папка word, в ней то и содержится основное содержимое файла DOCX. Как видно, все файлы и настройки в этом формате стремились сделать с расширением XML, это неспроста, ведь X на конце означает отношение к XML. На рисунке ниже можно увидеть содержимое папки word, главный файл здесь document.xml.

docx-structure-word

Именно файл document.xml и содержит весь основной контент документа DOCX. Работать с форматом XML достаточно просто при помощи любых средств, ведь этот формат напоминает обычный текст, только он четко структурирован, что позволяет легко обратиться в любую точку файла и изменить любые данные.

Чтобы открыть DOCX на PHP и записать туда нужные данные, можно использовать довольно простой код:

$docx = new ZipArchive(); if ($docx->open('path_to_file/file.docx') === true) < $xml = $docx->getFromName('word/document.xml'); $xml = str_replace('[ИСКОМЫЙ ТЕКСТ]', 'ЗАМЕЩАЮЩИЙ ТЕКСТ', $xml); $docx->addFromString('word/document.xml', $xml); $docx->close(); > 

Сначала создается объект ZIP архива, в который затем считывается содержимое DOCX, из DOCX извлекается контент document.xml. Затем происходит непосредственная запись данных в файл, просто ищется заранее подготовленное место в файле DOCX в виде токена [ИСКОМЫЙ ТЕКСТ] и на его место записываются любые данные. После выполнения этой операции, происходит запись обновленного document.xml в DOCX и архив закрывается.

Важно проследить за целостностью токенов, если текст заменятся таким способом. Сделать это можно достаточно просто. Сначала откройте нужный файл в Word и расставьте токены, желательно написать их в простом блокноте и затем вставлять на нужные позиции. После чего, сохраните файл DOCX и откройте его архиватором, найдите и откройте в нем document.xml – проверьте целостность токенов, они должны быть написаны слитно, без разрывов. Только в таком случае, возможна корректная замена токенов на нужный текст.

Таким образом, было рассмотрено устройство формата DOCX, а также показано, как можно легко записать данные в DOCX на PHP.

Источник

Оцените статью