Page 1 of 121
DocxFactory
Version 1.2.14
Python
Tutorial
Written by Alon Blich
DocxFactory Ltd.
www.docxfactory.com
Page 2 of 121
Table of Contents
Linux Installation ...................................................................................................................................... 4
Windows Installation ................................................................................................................................ 6
Printing and Conversion Tools Installation ............................................................................................... 8
Introduction .............................................................................................................................................. 9
Top Level Items ....................................................................................................................................... 10
Top Level Items Exercise .................................................................................................................... 10
docxfactory.WordProcessingCompiler Singleton ........................................................................... 15
docxfactory.WordProcessingCompiler.compile Method ............................................................... 15
docxfactory.WordProcessingMerger Singleton .............................................................................. 16
docxfactory.WordProcessingMerger.load Method ........................................................................ 18
docxfactory.WordProcessingMerger.paste Method ...................................................................... 18
docxFactory.WordProcessingMerger.save Method ....................................................................... 19
Sub Level Items ....................................................................................................................................... 22
Sub Level Items 1 Exercise ................................................................................................................. 22
Sub Level Items 2 Exercise ................................................................................................................. 26
Item Groups .................................................................................................................................... 26
Fields ....................................................................................................................................................... 30
Fields Exercise .................................................................................................................................... 30
Text Format .................................................................................................................................... 32
Number Format .............................................................................................................................. 32
Datetime Format ............................................................................................................................ 32
docxFactory.WordProcessingMerger.set_clipboard_value Method .............................................. 34
Special Fields .......................................................................................................................................... 37
Special Fields Exercise ........................................................................................................................ 37
Picture Format ................................................................................................................................ 43
Barcode Format .............................................................................................................................. 44
HTML/RTF Format .......................................................................................................................... 52
Boolean Format .............................................................................................................................. 52
Barcodes 1 Exercise ............................................................................................................................ 56
Barcodes 2 Exercise ............................................................................................................................ 59
Chart Fields ............................................................................................................................................. 65
Chart Fields Exercise .......................................................................................................................... 65
docxfactory.WordProcessingMerger.set_chart_value Method ..................................................... 69
DocxFactory Ltd.
www.docxfactory.com
Page 3 of 121
Merge XML and JSON ............................................................................................................................. 72
Merge XML and JSON Exercise ........................................................................................................... 72
docxfactory.WordProcessingMerger.merge Method .................................................................... 74
Border Conflicts ...................................................................................................................................... 78
Border Conflicts Exercise.................................................................................................................... 78
Alternating Colors ................................................................................................................................... 83
Alternating Colors Exercise ................................................................................................................ 83
_alternate ....................................................................................................................................... 85
Sections .................................................................................................................................................. 88
Sections Exercise ................................................................................................................................ 88
Table of Contents Automatic Update ..................................................................................................... 92
Table of Contents Automatic Update Exercise .................................................................................. 93
docxFactory.WordProcessingMerger.set_update_toc_method Method ...................................... 97
Introduction to Paging .......................................................................................................................... 100
Word Paging Features .......................................................................................................................... 101
Word Paging Exercise ....................................................................................................................... 101
Repeat Header Rows .................................................................................................................... 104
Keep with Next ............................................................................................................................. 104
Keep Lines Together ..................................................................................................................... 104
Allow row to break across pages .................................................................................................. 104
DocxFactory Paging Features ............................................................................................................... 107
DocxFactory Paging Exercise ............................................................................................................ 108
_pageSize ................................................................................................................................ 111
_size ........................................................................................................................................ 111
_keepTogether ............................................................................................................................. 112
_keepWithPrev ............................................................................................................................. 112
filler......................................................................................................................................... 112
spacer ..................................................................................................................................... 112
Third Party Acknowledgement ............................................................................................................. 115
DocxFactory Ltd.
www.docxfactory.com
Page 4 of 121
Linux Installation
This chapter shows you how to install and setup DocxFactory on Linux.
1. Go to www.docxfactory.com and download the Linux installation package.
For the Linux 32bit version download DocxFactoryLinux32.tar.gz.
For the Linux 64bit version download DocxFactoryLinux64.tar.gz.
2. Before starting, it is recommended that you login as root so you have all the necessary
permissions to perform the installation.
Enter the commands below in the command line.
$ su -
3.
Extract the DocxFactory/ directory into the /opt directory.
Enter the commands below in the command line.
# cd /opt
# tar -zxvf DocxFactoryLinux32.tar.gz
Note:
If there is an older version installed then delete the DocxFactory directory first
(do not overwrite it).
4. Add the DocxFactory/lib/ directory to the list of library directories to search when a program
is run.
Either permanently using ldconfig:
# echo /opt/DocxFactory/lib/ > /etc/ld.so.conf.d/DocxFactory.conf
# chmod 666 /etc/ld.so.conf.d/DocxFactory.conf
# ldconfig
Or temporarily using the LD_LIBRARY_PATH environment variable before the program is run:
# export LD_LIBRARY_PATH=/opt/DocxFactory/lib:$LD_LIBRARY_PATH
Note:
You will need to restart all processes that use the DocxFactory.so (or simply restart
the Computer) for the changes to take effect.
如果gcc报错: yum install gcc-c++
如果 import docxfactory出错,一定要将 DocxFactory安装在 /opt下
DocxFactory Ltd.
www.docxfactory.com
Page 5 of 121
5.
Install the DocxFactory Python module:
Enter the commands below in the command line.
# cd /opt/DocxFactory/python
# python setup.py install
Note:
The python-devel package (for the Python version you are using) must be installed to
compile the extension module (.so file). If the Python.h file exists in the Python
installation include directory then the python-devel package is most likely installed.
You can also create a binary distribution (using distutils) for the same Python version
with the compiled extension module that can be used to install the extension module
without the python-devel package.
DocxFactory Ltd.
www.docxfactory.com
Page 6 of 121
Windows Installation
This chapter shows you how to install and setup DocxFactory on Windows.
1. Go to www.docxfactory.com and download the Windows installation .ZIP file.
For the Windows 32bit version download DocxFactoryWin32.zip.
For the Windows 64bit version download DocxFactoryWin64.zip.
2.
Extract the DocxFactory\ directory into the Program Files\ directory.
The DocxFactoryWin32.zip is extracted to the C:\Program Files (x86)\ directory.
The DocxFactoryWin64.zip is extracted to the C:\Program Files\ directory.
Note:
If there is an older version installed then delete the DocxFactory directory first
(do not overwrite it). You may need to shutdown all processes using the
DocxFactory.dll or the file will be locked and undeletable.
3. Add the DocxFactory\bin\ directory to the Windows PATH used to search for executables
and libraries when a program is run.
1.
2.
3.
4.
Right click My Computer and click Properties.
In the System Properties window, click on the Advanced tab.
In the Advanced section, click the Environment Variables button.
In the Environment Variables dialog box, select the PATH variable in the Systems
Variable section, click the Edit button and add to the PATH the “C:\Program
Files\DocxFactory\bin;” directory with a semicolon to separate it from the other
directories (see picture below).
Note:
You will need to restart all processes that use the DocxFactory.dll (or
the Computer) for the changes in the PATH environment variable to take effect.
simply restart
DocxFactory Ltd.
www.docxfactory.com
Page 7 of 121
4.
Install the DocxFactory Python module.
Open the Command Prompt and enter the commands below.
C:> cd "C:\Program Files\DocxFactory\python"
C:> python setup.py install
Note:
Note:
Note:
The python-devel package (for the Python version you are using) must be installed to
compile the extension module (which is the default on Windows installation). If the
Python.h file exists in the Python installation include directory then the python-devel
package is most likely installed.
You will need a C++ compiler to compile the extension module (.pyd file) for the
Python version you are using. If you do not have a compiler installed, you can install
Microsoft Visual C++ Express.
You can also create a binary distribution (using distutils) for the same Python version
with the compiled extension module that can be used to install the extension module
without a compiler or the python-devel package.
DocxFactory Ltd.
www.docxfactory.com
Page 8 of 121
Printing and Conversion Tools Installation
DocxFactory can only creates .DOCX files directly. To create .PDF, .HTML or other file formats,
DocxFactory first creates a .DOCX file and then converts the file using conversion tools. Likewise
to print, DocxFactory uses printing tools.
For the printing and conversion tools DocxFactory uses:
Open Office (or LibreOffice) on Linux.
Microsoft Office on Windows (Open Office can also be used on Windows as a free alternative).
Note:
Installing printing and conversion tools is optional. If no printing and conversion tools are
installed then you will still be able to generate .DOCX files but not print or save as other file
formats.
Note: Open Office is not 100% compatible with Microsoft Word .DOCX file format meaning that it
may not look exactly the same in Open Office and may never will even in future versions of
Open Office simply because of the sheer size of the file format and all its different options. To
get the best results it is recommended to use Microsoft Office for printing and converting.
Note:
Future versions of DocxFactory will also support designing and creating Open Office .ODT files
directly that will give the best results when printing and converting on Linux.
To use Open Office you will also need to install the Open Office SDK (see link below).
http://api.libreoffice.org/docs/install.html
The ODF Converter Integrator is an add on for Open Office that greatly improves Open Office
compatibility with Microsoft Office file formats.
If you are using Open Office then it is highly
recommended that you also install the ODF Converter Integrator (see link below).
http://katana.oooninja.com/w/odf-converter-integrator
DocxFactory Ltd.
www.docxfactory.com