设为首页收藏本站

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 7523|回复: 7

PHPWORD: create word documents online

[复制链接]
发表于 2014-2-28 08:31:42 | 显示全部楼层 |阅读模式
本帖最后由 demo 于 2014-3-1 01:00 编辑

PHPWord is a library written in PHP that create word documents.
No Windows operating system is needed for usage because the result are docx files (Office Open XML) that can be opened by all major office software.

PHPWord is based on the fantastic PHPExcel / PHPPowerPoint libraries.
Major Features
  • Insert and format document sections
  • Insert and format Text elements
  • Insert Text breaks
  • Insert Page breaks
  • Insert and format Images and binary OLE-Objects
  • Insert and format watermarks (new)
  • Insert Header / Footer
  • Insert and format Tables
  • Insert native Titles and Table-of-contents
  • Insert and format List elements
  • Insert and format hyperlinks
  • Very simple template system (new)

See the Documentation for all details.

related projects
                    PHPExcel
                    PHPPowerPoint
                    HTML to docx Converter
               

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

×
 楼主| 发表于 2014-2-28 08:56:31 | 显示全部楼层
PHPOffice
PHPOffice contains libraries which permits to write and read files from most office suites. phpoffice

PHPExcelPHPExcel is a library written in pure PHP and providing a set of classes that allow you to write to and read from different spreadsheet file formats, like Excel (BIFF) .xls, Excel 2007 (OfficeOpenXML) .xlsx, CSV, Libre/OpenOffice Calc .ods, Gnumeric, PDF, HTML, ... This project is built around Microsoft's OpenXML standard and PHP.
View details »

PHPPowerPoint
View details »

PHPProject
View details »

PHPVisio
View details »

PHPWordPHPWord is a library written in pure PHP and providing a set of classes that allow you to write to and read from different document file formats, like Word .docx, WordPad .rtf, Libre/openOffice Writer .odt.
View details »



 楼主| 发表于 2014-2-28 09:21:53 | 显示全部楼层

DocxGenJS

docxgen.js is a small library to generate docx documents given a docx template. It can replace tags by their values and replace images with other images. It is very user oriented as users can without a lot of programming knowledge create their first template and automatically change variables in it.
Replace Variables
Formating
Loops
Loops and tables
Lists
Replacing images
Naming the output

Word does a lot of changes when opening a docx and saving it again, so I would'nt trust this. I don't even know where it would be possible to store hidden and persistent data inside document.xml.


Here's an idea to get what you need using an other technique.
Hello {name}with name="edi9999" will be replaced by Hello edi9999
{#names}Hello {name}{/names}with names=[{name:"John"},{name:"Mary"},{name:"Jane"}]
will be replaced by:
Hello JohnHello MaryHello JaneNow the trick to comment out a section is to use an empty array.
if names=[]
the output will be an empty string. If you want to uncomment it, use an array with one element.

I have created an implementation of this for Javascript (works on Node and in Browser): https://github.com/edi9999/docxgenjs
There's a demo Here:
http://javascript-ninja.fr/docxgenjs/examples/demo.html


 楼主| 发表于 2014-2-28 09:26:26 | 显示全部楼层

oodocx

本帖最后由 demo 于 2014-3-1 01:28 编辑

oodocx is a fork/modification/Python 3 port of Mike MacCana's excellent python-docx module. Here are some of the differences of oodocx from python-docx:
1. Object Oriented. As the name suggests, oodocx has an object-oriented aspect. The Docx class represents the various files that comprise a docx file, which is simply a zip file that contains primarily xml files. The document.xml file is the main document that holds most of the content that you find in a word document. However, this module also gives you easy access to most of the other files that typically comprise a docx file as attributes of the Docx class. For example, the root element in styles.xml of a Docx object named "d" can be accessed as d.styles. This allows for greater control of the look, feel, and metadata of a docx file.
2. Python 3 compatible.
3. Expanded functionality, particularly for people with limited knowledge of xml who want to get started quickly on their docx scripting projects. Some examples include the modify_font and modify_paragraph functions, which allow for easy modification of common font and paragraph properties of an element or a list of elements.
4. oodocx keeps all of the files in the Docx zip, rather than just the document.xml file. This helps to preserve formatting and ensures that pictures, comments, and other elements in a Docx won't break from save to save.

InstallationFirst, ensure that you have the appropriate version of lxml installed. Then, clone this repository, navigate your shell window to the oodocx directory folder that contains the setup.py file, and enter "python setup.py install" (or just "setup.py install", depending on how you execute python files on your computer)

How do I...First of all, be sure to check out the /examples folder for basic examples of this module


Create a new document, insert a paragraph of text, and save it
  1. d = oodocx.Docx()
  2. d.body.append(oodocx.paragraph('Hello world!'))
  3. d.save('hello.docx')
复制代码

Open a document, insert a paragraph after the paragraph containing the word "apple", and save it somewhere else
  1. d = oodocx.Docx(r'C:\users\applecart\apples.docx')
  2. apple_para = d.search('apple', result_type='paragraph')
  3. pos = body.index(apple_para) + 1 #lxml
  4. d.body.insert(pos, oodocx.paragraph('Bananas!')) #lxml
  5. d.save(r'C:\users\bananstand\there's always money here.docx')
复制代码


Note that the index() and insert() methods in the fourth and fifth lines of the above code are from the underlying lxml module. Check out the documentation here.

Here's a basic Python implementation using oodocx, a module that I'm currently developing.
  1. from oodocx import oodocx
  2. from lxml import etree

  3. d = oodocx.Docx('template.docx')
  4. body = d.get_body()
  5. paragraph_to_remove = d.search('Some text here', result_type='paragraph')
  6. body.remove(paragraph_to_remove)
  7. d.save('new document.docx')
复制代码

source

 楼主| 发表于 2014-2-28 14:38:28 | 显示全部楼层

TinyButStrong

TinyButStrong is a library that enables you to dynamically create XML/HTML pages and any other files based on text source. It's a Template Engine for the PHP language. It enables you to easily display information from your database, but also to seriously harmonize and simplify your PHP programming.
TinyButStrong is oriented to HTML but not specialized to Html. This means it can work as well with Text files, XML, RSS, RTF, WML, Excel (xml), ... The OpenTBS plug-in enables your to merge OpenOffice and Ms Office documents.

TinyButStrong's trumps:
  • simplicity : 1 file only, 1 Php class only with 6 methods and 5 properties.
  • visual / WYSIWYG editors (such as Dreamweaver or FrontPage) friendly
  • database friendly,
  • fast development,
Main features:
  • can connect to any database type (in native: MySql, SQLite, PostgreSQL),
  • easy date-time and numeric formats,
  • blocks with alternated display,
  • multi-columns display,
  • conditional display,
  • event functions,
  • file inclusion and other scripts execution,
  • cache system,
  • plug-ins.


TinyButStrong plug-ins Don't forget that most of plug-ins need be included and installed only just before to use them.
• Plug-ins provided with the standard TBS distribution (with the zip file). Changelog is here.

ByPage1.0.5 Enables the MergeBlock() method to display only a part of the records. Click here for more details
CacheSystem1.0.6Provides several actions for a Cache System. Click here for more details
NavBar1.0.6Displays a navigation bar. Click here for more details
HTML1.0.7Selecting HTML items and checking HTML contents. Click here for more details
MergeOnFly1.0.5Enables the MergeBlock() method to display data on fly. Click here for more details

• Plug-ins for CMS (Content Management System):
PmWiki2.0.2Dynamic Articles with TinyButStrong, a plugin for the CMS/Wiki pmWiki.
Manual and download| PmWiki home page
Joomla1.2.4Dynamic Articles with TinyButStrong, a plugin for the CMS Joomla.
Manual| Changelog | Download | Tutorials | Joomla home page
SPIP1.0.5Dynamic Articles with TinyButStrong, a plugin for the CMS SPIP.
Manual| Changelog |Download | SPIP home page
Drupal 61.5.0 betaDynamic Articles with TinyButStrong, a plugin for the CMS Drupal version 6.
Manual| Download | Drupal home page

• Plug-ins provided by the TBS team:
OpenTBS1.7.6OpenTBS is a PHP tool that easily merges both OpenOffice and Ms Office documents. In fact it can merge any XML or Text file saved in a zip container.
Demo | Manual | Download
Aggregate1.0.0Provides aggregate calulations when a block is merged. It does Sum, Min, Max, Avg, Count, Accumulation and Changes.
Demo | Manual| Download
Excel1.1.0 Makes TBS enables to merge Microsoft Excel Worksheets.
Demo | Manual | Download

• Other TBS plug-ins submitted by coders:
Plus1.1.0This plugin adds a number of HTML field parameter to TinyButStrong, making it easier to use magnet, do calculation, manipulate string, or author web page.
Posted by Ho Yiu Yeung (Sheepy)
Demo | Manual | Download
Export1.0.0The Export plugins allows you to "export" a block's field to the outside of the block.
Posted by Mick@el
Demo 1 | Demo 2 | Manual | Download
Php1.0Merges blocks and sublocks directly with the result of a PHP function or expression.
Posted by Jérémy. Download







 楼主| 发表于 2014-2-28 14:39:06 | 显示全部楼层

Feel free to submit your own intersting TBS plug-ins. Please send a package ready to be published, with a help file. Examples are also welcome.
Database plug-ins Here is some plug-ins that enables TinyButStrong to read data from your database.
If you have a new plug-in for a database which is not listed below, you can send it to me.

Remark: You should call the plug-in from your application and not adding it into the TinyButStrong file (tbs_class.php).

SourceWeb site Type TBS functions
ADOdb (John Lim)
Connectivity
ADODB (Microsoft)
Connectivity
in native until 1.97
CSV
File format
dbx
Connectivity
Doctrine
Connectivity
ezSQL
Connectivity
FireBird
Database
Informix
Database
Ingres
Database
InterBase
Database
MongoDB
Database NoSQL
MySQL
Database in native since 1.00
MySQLi
Connectivity in native since 3.7.0
ODBC
Connectivity
in native until 1.97
Oracle
Database
PDO (Php Data Objects)
Connectivity in native since 3.7.0
Pear:B
Connectivity
PostgreSQL
Database in native since 1.90
SQLite
Database in native since 1.96
SQL-Server
Database
in native until 1.97
Sybase
Database
TbsSQL
Connectivity in native since 3.2.0

 楼主| 发表于 2014-2-28 14:54:48 | 显示全部楼层

tinyDoc class

OverviewThe sfTinyDocPlugin allows to generate OpenOffice (OpenDocument) and Word 2007 documents with TinyButStrong template engine.
  • Prerequisite :

    • PHP 5.2
    • symfony framework 1.0, 1.1 and 1.2 (for the plugin)
  • History :
    The previous class was named tbsOOo. I rewrite the class to have a clean code with new methods and add some new functionnality like :

    • works with the last version of TinyButStrong
    • better support of specials characters like carriage return, new line and tab
    • can generate Word 2007 documents
    • can add images (only OpenDocument)
    • can merge data with native format in OpenOffice spreadsheet
    • two methods to zip and unzip the office files (zip with command line and the pecl ZipArchive)
    • and a plugin for symfony framework : sfTinyDocPlugin
  • The office formats supported :

    • OpenOffice 1.0 or OpenDocument 1.0
    • OpenOffice 2.0 or OpenDocument 1.1
    • Word 2007
  • It's useful :

    • To create word processing and spreadsheet documents.
    • To create reports, bill, orders.
    • To create mailing.
    • To create multi pages documents.
    • To use the power of the TinyButStrong template engine syntax :
      • Merge fields
      • Merge blocks (repetitive fields)
      • and more...
  • It's not :

    • To create an Office document from scratch.
    • To convert a document from OpenOffice to Word or to PDF or else. See the project PyODConverter to do that.
  • Avantages :

    • No installation of OpenOffice on the server side.
    • Works on *nix, win and other systems.
    • Templates can be created and modified easyly by designer.
    • Templates layouts and styles are made with OpenOffice or Word2007 application.
    • For the symfony framework
      • Works with sf1.0, sf1.1 and sf1.2
      • No dependance with ORM
      • Template have the same location as the symfony templates.
      • Template name have the same name as the symfony templates. moduleName/templates/actionNameSuccess.odt
      • Get the config from app.yml.
  • Only for OpenDocument (OpenOffice) :

    • Merge pictures dynamicaly with the new parameter named 'image'.
    • Data in spreadsheet are converted to native OpenOffice format with the new parameter named 'type'.
      • Useful to work with spreadsheet formulas
      • Useful to work the spreadsheet formats
    • Transform data to a linkable text with the new parameter named 'link'.
  • Limitations :

    • Can't merge collection of objects (iterator not supported in TinyButStrong, soon in next version), you have to transform to an array before.

      • For Propel write your own method like asArray() in the Jobeet tutorial, see day 16.
      • For Doctrine use the magic method : $result->toArray (false | true) to transform the result to an array, see : doctrine api
    • Can't change a style of the Office document by code, the styles are fix when you editing your Office document.
    • Can't merge global vars [var.xxx], use method mergeXmlField() in place of use globals vars
    • Can't merge sub-block because TinyButStrong need global vars, see headergrp syntax
Installation
  • Install sfTinyDocPlugin :
    $ pear channel-discover pear.symfony-project.com$ symfony plugin-install http://plugins.symfony-project.com/sfTinyDocPluginOr from the SVN repository
    $ svn co http://svn.symfony-project.com/plugins/sfTinyDocPlugin/trunk/
  • Optionally add the following config to app.yml :
    [yml]all:  sf_tiny_doc_plugin:    zip_method:    shell             # the method to zip/unzip : shell | ziparchive    zip_bin:       zip               # the binary to zip   for 'shell' method    unzip_bin:     unzip             # the binary to unzip for 'shell' method    process_dir:   %SF_WEB_DIR%/tmp  # the process directory
  • Clear your cache :
    $ ./symfony cc
  • Configure the method to zip/unzip office documents :

    • Method 1 : shell (by default)

      • To install Zip on Red Hat Enterprise Linux or CentOS
        $ yum install zip
      • To install Zip on Windows, download the zip binary and copy them onto your hard disk. It could be useful to modify your Windows global var path to use them without to specify the binary path.

    • Method 2 : ziparchive

      • To install Pecl ZipArchive on Red Hat Enterprise Linux or CentOS
        $ yum install httpd-devel   # if necessary$ pecl install zip
  • Create a directory where to process temporary files, by example on web root :
    $ mkdir web/tmp$ chmod 777 web/tmp

and for Word2007 document (.docx)
  • Change in your action
    $doc->createFrom();
    $doc->loadXml('content.xml');
    By
    $doc->createFrom(array('extension' => 'docx'));
    $doc->loadXml('word/document.xml');
  • Create an Word2007 document (.docx) and paste the same as before.
    $doc->mergeXmlField() with a string[field1]$doc->mergeXmlField() with an array[field2.id] [field2.name]$doc->mergeXmlField() with an object[field3.getZipMethod][field3.getZipBinary][field3.getUnzipBinary]$doc->mergeXmlBlock() with an array[block1;block=begin][block1.$] [block1.firstname] [block1.lastname][block1;block=end]Num rows : [block1.#]
  • Save the document as basicSuccess.docx in template directory doc/templates/
  • You can now test the Word2007 document in a browser the result : doc/basicWord
    Tips$doc->createFrom();  // the defaut extension is 'odt'
    $doc->loadXml();     // the defaut XML filename is 'content.xml'

The basic structure of OpenDocument (OpenOffice) and Word 2007 files
  • The documents are a zip archive format with these main files :
                      OpenDocument      Word2007content           content.xml       word/document.xmlmeta              meta.xml          ? settings          settings.xml      word/settings.xmlstyles            styles.xml        word/styles.xml     header            styles.xml        word/header1.xml     footer            styles.xml        word/footer1.xml     content.xml is the main file to merge in OpenOffice documents.
    word/document.xml is the main file to merge in Word 2007 documents.
    content.xml is the defaut file in loadXml() method.
    See more on :

  • The XML tags you have to know :
                HTML tags     OpenDocument XML tags     Word2007 XML tagstable       <table>       <table:table>             <w:tbl>row         <tr>          <table:table-row>         <w:tr>cell        <td>          <table:table-cell>        <w:tc>paragraph   <p>           <text:p>                  <w:p>
The method to merge data
  • Before merging data, you have to load the XML file with the loadXml() method.
  • After merging data, you have to save the result of merging in the XML file with the saveXml() method.
    Each time you have to merge data with an another file you have to
    $doc->loadXml('content.xml');
    // ...
    $doc->mergeXml(...);
    $doc->mergeXmlField(...);
    $doc->mergeXmlBlock(...);
    $doc->mergeXml(...);
    // ...
    $doc->saveXml();


    $doc->loadXml('styles.xml');
    // ...
    $doc->mergeXml(...);
    $doc->mergeXmlField(...);
    $doc->mergeXmlBlock(...);
    $doc->mergeXml(...);
    // ...
    $doc->saveXml();


For all parameters of TinyButStrong, have a look to the TinyButStrong manual

More information see TinyDoc

SupportMailing-list : http://groups.google.fr/group/tinydoc
Please email to the mailing-list tinydoc@googlegroups.com for support (register only).
Bug tracker : http://trac.symfony-project.org/browser/plugins/sfTinyDocPlugin
The tbsOOo class is no longer supported, and I strongly discourage his use.




本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

×
 楼主| 发表于 2014-2-28 15:16:48 | 显示全部楼层

SnippetMaster

SnippetMaster is a free easy to use content editing tool for website owners or designers who want a very simple and visual way to quickly edit pre-defined areas on any web page, through any browser, without messing up the rest of the web page. (SnippetMaster can also edit entire webpages and regular text files, too.)

This is not a open source but you can still use it for window browser editor and add your won back-end php code.




您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|小黑屋|BC Morning Website ( Best Deal Inc. 001 )  

GMT-8, 2025-8-26 13:51 , Processed in 0.017018 second(s), 18 queries .

Supported by Best Deal Online X3.5

© 2001-2025 Discuz! Team.

快速回复 返回顶部 返回列表