Editing Word Document, add Headers/footers and save it - Python -


i want add headers , footers every page of word document, , want add pages start of document, how can using python ? have tried python-docx not working expected. there other way achieve requirement ?

i think best way can take looking @ python-docx see how manages files. docx zipped format (docx tag wiki):

the .docx format zipped file contains following folders:

+--docprops |  +  app.xml |  \  core.xml +  res.log +--word //this folder contains of files control content of document |  +  document.xml //is actual content of document |  +  endnotes.xml |  +  fonttable.xml |  +  footer1.xml //containst elements in footer of document |  +  footnotes.xml |  +--media //this folder contains images embedded in word |  |  \  image1.jpeg |  +  settings.xml |  +  styles.xml |  +  styleswitheffects.xml |  +--theme |  |  \  theme1.xml |  +  websettings.xml |  \--_rels |     \  document.xml.rels //this document tells word images situated +  [content_types].xml \--_rels    \  .rels 

a library docx-python extracts @ first docx, find in python docx: have found you: https://github.com/mikemaccana/python-docx/blob/master/docx.py#l65

def opendocx(file):     '''open docx file, return document xml tree'''     mydoc = zipfile.zipfile(file)     xmlcontent = mydoc.read('word/document.xml')     document = etree.fromstring(xmlcontent)     return document 

you can xmlcontent of 'word/document.xml' main content of docx, change either using docx-python (which recommend you, docx-python seems able add many different elements. if want copy content of other word document @ beginning of document, try copy content of document.xml document.xml, give errors, specially if use images or non-text content.

to add header or footer, have create file word/header1.xml or word/footer1.xml copy header1.xml content of file created, should work.

hope helps


Comments

  1. I just found this blog and have high hopes for it to continue. Keep up the great work, its hard to find good ones. I have added to my favorites. Thank You. this

    ReplyDelete

Post a Comment

Popular posts from this blog

html5 - What is breaking my page when printing? -

c# - must be a non-abstract type with a public parameterless constructor in redis -

ajax - PHP/JSON Login script (Twitter style) not setting sessions -