使用python读写.docx文件 [英] Read and Write .docx file with python

查看:438
本文介绍了使用python读写.docx文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文件夹,其中包含几个名为[Code2001.docx, Code2002.docx... Code2154.docx] .docx 文件.

I have a folder containing several .docx files with names [Code2001.docx, Code2002.docx... Code2154.docx].

我正在尝试编写一个脚本,该脚本将:

I'm trying to write a script that will:

  1. 打开每个.docx文件
  2. 在文档上添加一行; 已选中"
  3. 将.docx文件保存到另一个名为"Code2001_checked"的文件夹中

搜索后,我仅设法通过循环获取文件名:

After searching I've only managed to get the filename with the loop:

import os
os.chdir(r"E:......\test")

for files in os.listdir("."):
    if files.endswith(".docx"):
        print filename

我还发现了这一点: docx模块,但是文档很难继续.

I also found this: docx module but the documentation is poor to continue.

关于如何完成此脚本的任何建议?

Any suggestions on how to finish this script?

推荐答案

from docx import *
document = opendocx("document.doc")
body = document.xpath('/w:document/w:body', namespaces=nsprefixes)[0]
body.append(paragraph('Appending this.'))

第二行可能需要碰巧,具体取决于您要在文件中附加文本的位置.要完成此操作,您将需要使用savedocx()函数,并且在项目根目录中有一个用法示例.

The second line may need to chance depending on where in the file you are going to append the text. To finish this, you will need to use the savedocx() function, and there is an example of its usage in the root of the project.

这篇关于使用python读写.docx文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆