码迷,mamicode.com
首页 > 编程语言 > 详细

python读取word表格内容(1)

时间:2015-12-04 12:43:46      阅读:275      评论:0      收藏:0      [点我收藏+]

标签:

1.首页介绍下word表格内容,实例如下: 每两个表格后面是一个合并的单元格

技术分享

2.引入win32com模块

3.具体代码

技术分享
 1 #http://www.jb51.net/article/70318.htm
 2 #http://www.cnblogs.com/graphics/articles/2953665.html
 3 #http://shouce.jb51.net/python/
 4 import win32com,re
 5 from win32com.client import Dispatch,constants
 6 
 7 
 8 word=win32com.client.Dispatch(word.application)
 9 ‘‘‘
10 设置Word的可见性visible,默认情况下,你看不到Word程序;然后设置Word的警告信息是否出现,默认也是不出现,这样你在使用python控制Word的时候不会弹出Word的警告信息。
11 ‘‘‘
12 word.displayalerts=0
13 word.visible=0
14 countdoc=word.Documents.Count
15 print(countdoc)
16 doc=word.Documents.Open(rC:\Users\Administrator\Desktop\test\文档一\1.doc)
17 #doc.SaveAs(r‘C:\Users\Administrator\Desktop\test\文档一\1.txt‘)
18 ‘‘‘
19 t=doc.Tables[0]
20 #print(type(t))
21 tt=str(t)
22 #print(type(tt))
23 #print("")
24 #分割字符串
25 strs=tt.split(‘‘)
26 print(strs[5])
27 ‘‘‘
28 i=0
29 while i<500:
30     t=doc.Tables[i]
31     tt=str(t)
32     strs=tt.split()
33     print(strs[5])
34     i=i+1
35 
36 doc.Close()
37 word.Quit()
pythonword1

 

4.遇见的问题

1)。打开word总提示错误。原因是我Documents和Open首字母小写了

技术分享

2)。

技术分享

我是直接把表格获取的对象转为字符串来处理

python读取word表格内容(1)

标签:

原文地址:http://www.cnblogs.com/kllay/p/python-word.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!