diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..412eeda --- /dev/null +++ b/.gitattributes @@ -0,0 +1,22 @@ +# Auto detect text files and perform LF normalization +* text=auto + +# Custom for Visual Studio +*.cs diff=csharp +*.sln merge=union +*.csproj merge=union +*.vbproj merge=union +*.fsproj merge=union +*.dbproj merge=union + +# Standard to msysgit +*.doc diff=astextplain +*.DOC diff=astextplain +*.docx diff=astextplain +*.DOCX diff=astextplain +*.dot diff=astextplain +*.DOT diff=astextplain +*.pdf diff=astextplain +*.PDF diff=astextplain +*.rtf diff=astextplain +*.RTF diff=astextplain diff --git a/.gitignore b/.gitignore index 352fe3b..d6636f4 100644 --- a/.gitignore +++ b/.gitignore @@ -3,236 +3,7 @@ *.docx *.kpf build -problems -template/word/media -bin/activate -bin/activate_this.py -bin/easy_install -bin/easy_install-2.6 -bin/get_env_details -bin/pilconvert.py -bin/pildriver.py -bin/pilfile.py -bin/pilfont.py -bin/pilprint.py -bin/pip -bin/postactivate -bin/postdeactivate -bin/preactivate -bin/predeactivate -bin/python -bin/python2.6 -include/python2.6 -lib/python2.6/UserDict.py -lib/python2.6/UserDict.pyo -lib/python2.6/_abcoll.py -lib/python2.6/_abcoll.pyo -lib/python2.6/abc.py -lib/python2.6/abc.pyo -lib/python2.6/codecs.py -lib/python2.6/codecs.pyo -lib/python2.6/config -lib/python2.6/copy_reg.py -lib/python2.6/copy_reg.pyo -lib/python2.6/distutils/__init__.py -lib/python2.6/distutils/distutils.cfg -lib/python2.6/encodings -lib/python2.6/fnmatch.py -lib/python2.6/fnmatch.pyo -lib/python2.6/genericpath.py -lib/python2.6/genericpath.pyo -lib/python2.6/lib-dynload -lib/python2.6/linecache.py -lib/python2.6/linecache.pyo -lib/python2.6/locale.py -lib/python2.6/locale.pyo -lib/python2.6/ntpath.py -lib/python2.6/ntpath.pyo -lib/python2.6/orig-prefix.txt -lib/python2.6/os.py -lib/python2.6/os.pyo -lib/python2.6/posixpath.py -lib/python2.6/posixpath.pyo -lib/python2.6/re.py -lib/python2.6/re.pyo -lib/python2.6/site-packages/PIL.pth -lib/python2.6/site-packages/PIL/ArgImagePlugin.py -lib/python2.6/site-packages/PIL/BdfFontFile.py -lib/python2.6/site-packages/PIL/BmpImagePlugin.py -lib/python2.6/site-packages/PIL/BufrStubImagePlugin.py -lib/python2.6/site-packages/PIL/ContainerIO.py -lib/python2.6/site-packages/PIL/CurImagePlugin.py -lib/python2.6/site-packages/PIL/DcxImagePlugin.py -lib/python2.6/site-packages/PIL/EpsImagePlugin.py -lib/python2.6/site-packages/PIL/ExifTags.py -lib/python2.6/site-packages/PIL/FitsStubImagePlugin.py -lib/python2.6/site-packages/PIL/FliImagePlugin.py -lib/python2.6/site-packages/PIL/FontFile.py -lib/python2.6/site-packages/PIL/FpxImagePlugin.py -lib/python2.6/site-packages/PIL/GbrImagePlugin.py -lib/python2.6/site-packages/PIL/GdImageFile.py -lib/python2.6/site-packages/PIL/GifImagePlugin.py -lib/python2.6/site-packages/PIL/GimpGradientFile.py -lib/python2.6/site-packages/PIL/GimpPaletteFile.py -lib/python2.6/site-packages/PIL/GribStubImagePlugin.py -lib/python2.6/site-packages/PIL/Hdf5StubImagePlugin.py -lib/python2.6/site-packages/PIL/IcnsImagePlugin.py -lib/python2.6/site-packages/PIL/IcoImagePlugin.py -lib/python2.6/site-packages/PIL/ImImagePlugin.py -lib/python2.6/site-packages/PIL/Image.py -lib/python2.6/site-packages/PIL/ImageChops.py -lib/python2.6/site-packages/PIL/ImageCms.py -lib/python2.6/site-packages/PIL/ImageColor.py -lib/python2.6/site-packages/PIL/ImageDraw.py -lib/python2.6/site-packages/PIL/ImageDraw2.py -lib/python2.6/site-packages/PIL/ImageEnhance.py -lib/python2.6/site-packages/PIL/ImageFile.py -lib/python2.6/site-packages/PIL/ImageFileIO.py -lib/python2.6/site-packages/PIL/ImageFilter.py -lib/python2.6/site-packages/PIL/ImageFont.py -lib/python2.6/site-packages/PIL/ImageGL.py -lib/python2.6/site-packages/PIL/ImageGrab.py -lib/python2.6/site-packages/PIL/ImageMath.py -lib/python2.6/site-packages/PIL/ImageMode.py -lib/python2.6/site-packages/PIL/ImageOps.py -lib/python2.6/site-packages/PIL/ImagePalette.py -lib/python2.6/site-packages/PIL/ImagePath.py -lib/python2.6/site-packages/PIL/ImageQt.py -lib/python2.6/site-packages/PIL/ImageSequence.py -lib/python2.6/site-packages/PIL/ImageShow.py -lib/python2.6/site-packages/PIL/ImageStat.py -lib/python2.6/site-packages/PIL/ImageTk.py -lib/python2.6/site-packages/PIL/ImageTransform.py -lib/python2.6/site-packages/PIL/ImageWin.py -lib/python2.6/site-packages/PIL/ImtImagePlugin.py -lib/python2.6/site-packages/PIL/IptcImagePlugin.py -lib/python2.6/site-packages/PIL/JpegImagePlugin.py -lib/python2.6/site-packages/PIL/McIdasImagePlugin.py -lib/python2.6/site-packages/PIL/MicImagePlugin.py -lib/python2.6/site-packages/PIL/MpegImagePlugin.py -lib/python2.6/site-packages/PIL/MspImagePlugin.py -lib/python2.6/site-packages/PIL/OleFileIO.py -lib/python2.6/site-packages/PIL/PIL-1.1.7-py2.6.egg-info/PKG-INFO -lib/python2.6/site-packages/PIL/PIL-1.1.7-py2.6.egg-info/SOURCES.txt -lib/python2.6/site-packages/PIL/PIL-1.1.7-py2.6.egg-info/dependency_links.txt -lib/python2.6/site-packages/PIL/PIL-1.1.7-py2.6.egg-info/installed-files.txt -lib/python2.6/site-packages/PIL/PIL-1.1.7-py2.6.egg-info/top_level.txt -lib/python2.6/site-packages/PIL/PSDraw.py -lib/python2.6/site-packages/PIL/PaletteFile.py -lib/python2.6/site-packages/PIL/PalmImagePlugin.py -lib/python2.6/site-packages/PIL/PcdImagePlugin.py -lib/python2.6/site-packages/PIL/PcfFontFile.py -lib/python2.6/site-packages/PIL/PcxImagePlugin.py -lib/python2.6/site-packages/PIL/PdfImagePlugin.py -lib/python2.6/site-packages/PIL/PixarImagePlugin.py -lib/python2.6/site-packages/PIL/PngImagePlugin.py -lib/python2.6/site-packages/PIL/PpmImagePlugin.py -lib/python2.6/site-packages/PIL/PsdImagePlugin.py -lib/python2.6/site-packages/PIL/SgiImagePlugin.py -lib/python2.6/site-packages/PIL/SpiderImagePlugin.py -lib/python2.6/site-packages/PIL/SunImagePlugin.py -lib/python2.6/site-packages/PIL/TarIO.py -lib/python2.6/site-packages/PIL/TgaImagePlugin.py -lib/python2.6/site-packages/PIL/TiffImagePlugin.py -lib/python2.6/site-packages/PIL/TiffTags.py -lib/python2.6/site-packages/PIL/WalImageFile.py -lib/python2.6/site-packages/PIL/WmfImagePlugin.py -lib/python2.6/site-packages/PIL/XVThumbImagePlugin.py -lib/python2.6/site-packages/PIL/XbmImagePlugin.py -lib/python2.6/site-packages/PIL/XpmImagePlugin.py -lib/python2.6/site-packages/PIL/__init__.py -lib/python2.6/site-packages/PIL/_imaging.so -lib/python2.6/site-packages/PIL/_imagingmath.so -lib/python2.6/site-packages/PIL/_imagingtk.so -lib/python2.6/site-packages/easy-install.pth -lib/python2.6/site-packages/lxml-2.3beta1-py2.6.egg-info/PKG-INFO -lib/python2.6/site-packages/lxml-2.3beta1-py2.6.egg-info/SOURCES.txt -lib/python2.6/site-packages/lxml-2.3beta1-py2.6.egg-info/dependency_links.txt -lib/python2.6/site-packages/lxml-2.3beta1-py2.6.egg-info/installed-files.txt -lib/python2.6/site-packages/lxml-2.3beta1-py2.6.egg-info/not-zip-safe -lib/python2.6/site-packages/lxml-2.3beta1-py2.6.egg-info/top_level.txt -lib/python2.6/site-packages/lxml/ElementInclude.py -lib/python2.6/site-packages/lxml/__init__.py -lib/python2.6/site-packages/lxml/_elementpath.py -lib/python2.6/site-packages/lxml/builder.py -lib/python2.6/site-packages/lxml/cssselect.py -lib/python2.6/site-packages/lxml/doctestcompare.py -lib/python2.6/site-packages/lxml/etree.so -lib/python2.6/site-packages/lxml/html/ElementSoup.py -lib/python2.6/site-packages/lxml/html/__init__.py -lib/python2.6/site-packages/lxml/html/_dictmixin.py -lib/python2.6/site-packages/lxml/html/_diffcommand.py -lib/python2.6/site-packages/lxml/html/_html5builder.py -lib/python2.6/site-packages/lxml/html/_setmixin.py -lib/python2.6/site-packages/lxml/html/builder.py -lib/python2.6/site-packages/lxml/html/clean.py -lib/python2.6/site-packages/lxml/html/defs.py -lib/python2.6/site-packages/lxml/html/diff.py -lib/python2.6/site-packages/lxml/html/formfill.py -lib/python2.6/site-packages/lxml/html/html5parser.py -lib/python2.6/site-packages/lxml/html/soupparser.py -lib/python2.6/site-packages/lxml/html/usedoctest.py -lib/python2.6/site-packages/lxml/isoschematron/__init__.py -lib/python2.6/site-packages/lxml/isoschematron/resources/rng/iso-schematron.rng -lib/python2.6/site-packages/lxml/isoschematron/resources/xsl/RNG2Schtrn.xsl -lib/python2.6/site-packages/lxml/isoschematron/resources/xsl/XSD2Schtrn.xsl -lib/python2.6/site-packages/lxml/isoschematron/resources/xsl/iso-schematron-xslt1/iso_abstract_expand.xsl -lib/python2.6/site-packages/lxml/isoschematron/resources/xsl/iso-schematron-xslt1/iso_dsdl_include.xsl -lib/python2.6/site-packages/lxml/isoschematron/resources/xsl/iso-schematron-xslt1/iso_schematron_message.xsl -lib/python2.6/site-packages/lxml/isoschematron/resources/xsl/iso-schematron-xslt1/iso_schematron_skeleton_for_xslt1.xsl -lib/python2.6/site-packages/lxml/isoschematron/resources/xsl/iso-schematron-xslt1/iso_svrl_for_xslt1.xsl -lib/python2.6/site-packages/lxml/isoschematron/resources/xsl/iso-schematron-xslt1/readme.txt -lib/python2.6/site-packages/lxml/objectify.so -lib/python2.6/site-packages/lxml/pyclasslookup.py -lib/python2.6/site-packages/lxml/sax.py -lib/python2.6/site-packages/lxml/usedoctest.py -lib/python2.6/site-packages/pip-0.7.2-py2.6.egg/EGG-INFO/PKG-INFO -lib/python2.6/site-packages/pip-0.7.2-py2.6.egg/EGG-INFO/SOURCES.txt -lib/python2.6/site-packages/pip-0.7.2-py2.6.egg/EGG-INFO/dependency_links.txt -lib/python2.6/site-packages/pip-0.7.2-py2.6.egg/EGG-INFO/entry_points.txt -lib/python2.6/site-packages/pip-0.7.2-py2.6.egg/EGG-INFO/not-zip-safe -lib/python2.6/site-packages/pip-0.7.2-py2.6.egg/EGG-INFO/top_level.txt -lib/python2.6/site-packages/pip-0.7.2-py2.6.egg/pip/__init__.py -lib/python2.6/site-packages/pip-0.7.2-py2.6.egg/pip/backwardcompat.py -lib/python2.6/site-packages/pip-0.7.2-py2.6.egg/pip/basecommand.py -lib/python2.6/site-packages/pip-0.7.2-py2.6.egg/pip/baseparser.py -lib/python2.6/site-packages/pip-0.7.2-py2.6.egg/pip/commands/__init__.py -lib/python2.6/site-packages/pip-0.7.2-py2.6.egg/pip/commands/bundle.py -lib/python2.6/site-packages/pip-0.7.2-py2.6.egg/pip/commands/completion.py -lib/python2.6/site-packages/pip-0.7.2-py2.6.egg/pip/commands/freeze.py -lib/python2.6/site-packages/pip-0.7.2-py2.6.egg/pip/commands/help.py -lib/python2.6/site-packages/pip-0.7.2-py2.6.egg/pip/commands/install.py -lib/python2.6/site-packages/pip-0.7.2-py2.6.egg/pip/commands/search.py -lib/python2.6/site-packages/pip-0.7.2-py2.6.egg/pip/commands/uninstall.py -lib/python2.6/site-packages/pip-0.7.2-py2.6.egg/pip/commands/unzip.py -lib/python2.6/site-packages/pip-0.7.2-py2.6.egg/pip/commands/zip.py -lib/python2.6/site-packages/pip-0.7.2-py2.6.egg/pip/exceptions.py -lib/python2.6/site-packages/pip-0.7.2-py2.6.egg/pip/index.py -lib/python2.6/site-packages/pip-0.7.2-py2.6.egg/pip/locations.py -lib/python2.6/site-packages/pip-0.7.2-py2.6.egg/pip/log.py -lib/python2.6/site-packages/pip-0.7.2-py2.6.egg/pip/req.py -lib/python2.6/site-packages/pip-0.7.2-py2.6.egg/pip/runner.py -lib/python2.6/site-packages/pip-0.7.2-py2.6.egg/pip/util.py -lib/python2.6/site-packages/pip-0.7.2-py2.6.egg/pip/vcs/__init__.py -lib/python2.6/site-packages/pip-0.7.2-py2.6.egg/pip/vcs/bazaar.py -lib/python2.6/site-packages/pip-0.7.2-py2.6.egg/pip/vcs/git.py -lib/python2.6/site-packages/pip-0.7.2-py2.6.egg/pip/vcs/mercurial.py -lib/python2.6/site-packages/pip-0.7.2-py2.6.egg/pip/vcs/subversion.py -lib/python2.6/site-packages/pip-0.7.2-py2.6.egg/pip/venv.py -lib/python2.6/site-packages/setuptools-0.6c11-py2.6.egg -lib/python2.6/site-packages/setuptools.pth -lib/python2.6/site.py -lib/python2.6/sre.py -lib/python2.6/sre.pyo -lib/python2.6/sre_compile.py -lib/python2.6/sre_compile.pyo -lib/python2.6/sre_constants.py -lib/python2.6/sre_constants.pyo -lib/python2.6/sre_parse.py -lib/python2.6/sre_parse.pyo -lib/python2.6/stat.py -lib/python2.6/stat.pyo -lib/python2.6/types.py -lib/python2.6/types.pyo -lib/python2.6/warnings.py -lib/python2.6/warnings.pyo \ No newline at end of file + +*~ +\#*\# +.\#* diff --git a/docx.py b/docx.py index 0f41b09..6789cb3 100755 --- a/docx.py +++ b/docx.py @@ -7,26 +7,38 @@ See LICENSE for licensing information. ''' + import logging -from lxml import etree -try: - from PIL import Image -except ImportError: - import Image -import zipfile +import sys import shutil import re import time import os +import zipfile +from distutils import dir_util from os.path import join +from copy import deepcopy +from distutils import dir_util +from lxml import etree +try: + from PIL import Image +except ImportError: + try: + import Image + except ImportError: + sys.stderr.write("Can't find module PIL or Image") + + log = logging.getLogger(__name__) # Record template directory's location which is just 'template' for a docx # developer or 'site-packages/docx-template' if you have installed docx -template_dir = join(os.path.dirname(__file__),'docx-template') # installed -if not os.path.isdir(template_dir): - template_dir = join(os.path.dirname(__file__),'template') # dev +TEMPLATE_DIR = join(os.path.dirname(__file__), 'docx-template') # installed +if not os.path.isdir(TEMPLATE_DIR): + TEMPLATE_DIR = join(os.path.dirname(__file__), 'template') # dev + +TEMP_TEMPLATE_DIR = join(os.path.dirname(__file__), '.temp_template_dir') # All Word prefixes / namespace matches used in document.xml & core.xml. # LXML doesn't actually use prefixes (just the real namespace) , but these @@ -68,10 +80,23 @@ def opendocx(file): return document def newdocument(): + build_temp_template_layout() + document = makeelement('document') document.append(makeelement('body')) return document +def build_temp_template_layout(): + clear_temp_template_layout() + dir_util.copy_tree(TEMPLATE_DIR, TEMP_TEMPLATE_DIR) + +def clear_temp_template_layout(): + if os.path.exists(TEMP_TEMPLATE_DIR): + if os.path.isdir(TEMP_TEMPLATE_DIR): + shutil.rmtree(TEMP_TEMPLATE_DIR) + else: + os.remove(TEMP_TEMPLATE_DIR) + def makeelement(tagname,tagtext=None,nsprefix='w',attributes=None,attrnsprefix=None): '''Create an element & return it''' # Deal with list of nsprefix by making namespacemap @@ -132,7 +157,7 @@ def pagebreak(type='page', orient='portrait'): pagebreak.append(pPr) return pagebreak -def paragraph(paratext,style='BodyText',breakbefore=False,jc='left'): +def paragraph(paratext, style='BodyText', breakbefore=False, jc='left'): '''Make a new paragraph element, containing a run, and some text. Return the paragraph element. @@ -143,28 +168,33 @@ def paragraph(paratext,style='BodyText',breakbefore=False,jc='left'): If paratext is a list, spawn multiple run/text elements. Support text styles (paratext must then be a list of lists in the form - /