获取文件中表格并按表格形式输出

import re
import linecache
import docx
import sys
import docx
from docx import Document #导入库
import prettytable as pt
import xlrd
import xlwt

path = "D:\\文件\\政策汇\\有用\\untitled3\\docx\\b.docx" #文件路径
document = Document(path) #读入文件
tables = document.tables #获取文件中的表格集
tb = pt.PrettyTable()
book = xlwt.Workbook(encoding = 'utf-8')
test1 = book.add_sheet(u'test',cell_overwrite_ok = True)
for i in range(1,2):
table = tables[i]#获取文件中的第i-1个表格
if table:
# tb.field_names = [1,2]
result = []
for j in range(0,len(table.rows)):#从表格第二行开始循环读取表格数据
# result = table.cell(i,0).text + " " +table.cell(i,1).text + " "+table.cell(i,2).text +" "+ table.cell(i,3).text
#cell(i,0)表示第(i+1)行第1列数据,以此类推
for a in range(0,len(table.columns)):
result = table.cell(j,a).text
test1.write(j,a,result)
result.append(table.cell(j,a).text)
if j == 0:
tb.field_names = result
result.clear()
else:
tb.add_row(result)
result.clear()
print(tb)
posted @ 2019-08-13 16:35  orangeYY  阅读(235)  评论(0编辑  收藏  举报