Get the file in table form table and press output

import re
import linecache
import docx
import sys
import docx
from docx import Document # import library
import prettytable as pt
import xlrd
import xlwt

path = "D: \\ Files \\ \\ Useful policy remit \\ untitled3 \\ docx \\ b.docx" # file path
document = Document (path) # read the file
tables = document.tables # get file table set
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] # acquired first file tables i-1
if table:
# tb.field_names = [1,2]
result = []
for j in range (0, len (table.rows)): # cycle the second line read table data from the table
# result = table.cell(i,0).text + " " +table.cell(i,1).text + " "+table.cell(i,2).text +" "+ table.cell(i,3).text
#cell (i, 0) represents the (i + 1) row 1 column of data, and so on
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)

Guess you like

Origin www.cnblogs.com/zyl-kx/p/11346756.html