安装python的docx支持
python -m pip install python-docx
# -*- coding: utf-8 -*-
import docx
from docx import Document
path = \"./file2.docx\"
document = Document(path)
def readlist():
table_count = 0
row_count = 0
textlist = []
for paragraph in document.paragraphs:
r_text = paragraph.text
textlist.append(r_text)
row_count = row_count+1
if r_text.startswith(u\'表名\'):
# print row_count
tablenamec = textlist[row_count-2]
tablenamearr = textlist[row_count-1].split(u\':\')
tablename = tablenamearr[len(tablenamearr)-1]
print tablenamec,tablename
readtable(table_count)
textlist = []
row_count=0
table_count =table_count+1
def readtable(table_count):
# 遍历所有表格
tables = document.tables
table = tables[table_count]
rowlen = len(table.rows)
if rowlen>1:
collen = len(table.rows[0].cells)
row_num = 0
for row in table.rows:
if(row_num>0):
colname = row.cells[1].text
colnamec = row.cells[2].text
bz = \"\"
if(collen>4):
bz = row.cells[4].text
else:
bz = row.cells[3].text
sql = u\"insert into tablename (a,b,c,d) values(\'{0}\',\'{1}\',\'{2}\',\'{3}\')\".format(row_num,colname,colnamec ,bz)
# print sql
row_num = row_num +1
#
# for table in document.tables:
# for row in table.rows:
# print row.cells[2].text
if __name__ ==\"__main__\":
print \"start\"
readlist()