python-docx库读取txt文档,写入表格中
# -*- coding: utf-8 -*-
"""
Created on Mon Nov 9 20:45:49 2020
@author: Administrator
"""
from docx import Document
dstr=[]
f = open('C://111.txt')
d=f.read()
p1=0
p2=0
while(p2!=len(d)):
while( not (d[p2]=='\n' or d[p2]==' ')):
p2=p2+1
str=d[p1:p2]
dstr.append(str)
p2=p2+1
p1=p2
data=[]
time=[]
subject=[]
name=[]
p=0
while(p!=len(dstr)):
data.append(dstr[p])
time.append(dstr[p+1])
subject.append(dstr[p+2])
p=p+3
namestr=""
while(dstr[p][0]!='1'):
namestr=namestr+dstr[p]+" "
p=p+1
if(p==len(dstr)):
break
name.append(namestr)
doc=Document('C://p.docx')
tb=doc.tables[0]
i=0
while(i!=len(data)):
tb.cell(i+1,1).text=data[i]
tb.cell(i+1,2).text=time[i]
tb.cell(i+1,3).text=subject[i]
tb.cell(i+1,4).text=name[i]
i=i+1
doc.save('C://p231.docx')