[Python] python-docx 编辑word文档

[目的]
使用Python编辑Micorsoft Word 文档
[测试环境]
        OS: MS XP professional SP3 Win32
        Python:2.6.6
[准备]
        python-docx : 下载地址
         [Python] word 文档 - zzq635 - 未名
 [安装]
          

python setup.py install

 
[Python] word 文档 - zzq635 - 未名
[疑问]
        1.可能未装lxml module造成的.请看一下我写的  Window 安装 lxml,或 Google
[测试]
         脚本代码

#coding=UTF-8 from docx import * def testmakedocument(): relationships = relationshiplist() document = newdocument() body = document.xpath('/w:document/w:body', namespaces=nsprefixes)[0] body.append(heading(u"文件和文件夹服务接口", 1)) body.append(paragraph(u'请求格式')) request_rows = [ ['http method', 'url', u'说明'] , ['POST', 'http://www.163.com', 'B3'] ] body.append(table(request_rows)) body.append(paragraph(u'参数说明')) body.append(paragraph(u'相应内容')) body.append(paragraph(u'示例')) body.append(heading(u"上传文件", 1)) title = u'空间服务器接口说明文档' subject = u'空间服务器接口说明' creator = u'未名' keywords = ['alfresco', 'url', 'json'] coreprops = coreproperties(title=title, subject=subject, creator=creator, keywords=keywords) # Save our document savedocx(document, coreprops, appproperties(), contenttypes(), websettings(), wordrelationships(relationships), u'空间服务器接口说明文档.docx') def testUnit(): testmakedocument() if __name__ == '__main__': testUnit()

         结果展示
          [Python] word 文档 - zzq635 - 未名
 [疑问]
         1.如果发现Import Error: no module named Image  请安装  PIL.  推荐使用 PIP 仅使用一句话 :  pip install pil 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值