# -*- coding: utf-8 -*-
# from __future__ import print_function
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
import win32com
from win32com.client import Dispatch, constants
w = win32com.client.Dispatch('Word.Application')
w.Visible = 1
w.Documents.Open( FileName = 'D:\muzi\\37.docx') #路径不能存在中文
doc = w.ActiveDocument
count = doc.Tables.Count #统计整个word文档有多少个表
a = doc.Tables(1).rows.Count # 返回表格的行数
b = doc.Tables(1).columns.Count # 返回表格的列数
import collections
Content = collections.OrderedDict()
for num in range (count):
for i in range (doc.Tables(num+1).rows.Count):
key = re.sub(r'\r\x07','',doc.Tables(num+1).Cell(Row= i+1,Column = 1).Range.Text) //正则化去除特殊符号
value = re.sub(r'\r\07','',doc.Tables(num+1).Cell(Row=i+1,Column = 2).Range.Text)
Content[key] = value
filename = 'D:\muzi\m'
import json
with open(filename+'.json','a') as outfile: //打开已存在的文件
json.dump(Content,outfile,ensure_ascii = False) //字典内容写入json文件中
outfile.write('\n')