python-docx文件定位读取过程(尝试替换)
时间:2021-03-17 09:41:09|栏目:Python代码|点击: 次
以上是开头,安装完后需要导入转载的代码读取所有docx文件中的内容发现没有读取到表格数据:
from docx import Document
def readDocx(docName):
fullText = []
doc = docx.Document(docName)
paras = doc.paragraphs
for p in paras:
fullText.append(p.text)
return '\n'.join(fullText)
尝试精确定位第一个表格中第一个单元格的数据(只有一个表格也会读取为数组):
doc = docx.Document('04.docx')
print doc.tables[0].rows[0].cells[0].text
替换语句:
pname=unicode(context.rows[4].cells[1].text) #因为中文不兼容所以用unicode或者u
save语句:
resoult=doc.save(u'guochengwendang/04 DAEQP18-PS-04工作签到表.docx')
doc属性doc.paragraphs 取内容(不包括表格等特别因素)
doc.paragraphs sStr1= doc.paragraphs[0].text s=sStr1.replace(u"JKZX",unicode(context.rows[5].cells[1].text)) doc.paragraphs[0].text=s
栏 目:Python代码
下一篇:python搭建服务器实现两个Android客户端间收发消息
本文标题:python-docx文件定位读取过程(尝试替换)
本文地址:http://www.codeinn.net/misctech/82534.html






