python读取word文档的方法
作者:守株待兔 时间:2023-11-24 08:56:28
本文实例讲述了python读取word文档的方法。分享给大家供大家参考。具体如下:
首先下载安装win32com
from win32com import client as wc
word = wc.Dispatch('Word.Application')
doc = word.Documents.Open('c:/test')
doc.SaveAs('c:/test.text', 2)
doc.Close()
word.Quit()
这种方式产生的text文档,不能用python用普通的r方式读取,为了让python可以用r方式读取,应当写成
doc.SaveAs('c:/test', 4)
注意:系统执行完成后,会自动产生文件后缀txt(虽然没有指明后缀)。
在xp系统下面,应当,
open(r'c:\text','r')
wdFormatDocument = 0
wdFormatDocument97 = 0
wdFormatDocumentDefault = 16
wdFormatDOSText = 4
wdFormatDOSTextLineBreaks = 5
wdFormatEncodedText = 7
wdFormatFilteredHTML = 10
wdFormatFlatXML = 19
wdFormatFlatXMLMacroEnabled = 20
wdFormatFlatXMLTemplate = 21
wdFormatFlatXMLTemplateMacroEnabled = 22
wdFormatHTML = 8
wdFormatPDF = 17
wdFormatRTF = 6
wdFormatTemplate = 1
wdFormatTemplate97 = 1
wdFormatText = 2
wdFormatTextLineBreaks = 3
wdFormatUnicodeText = 7
wdFormatWebArchive = 9
wdFormatXML = 11
wdFormatXMLDocument = 12
wdFormatXMLDocumentMacroEnabled = 13
wdFormatXMLTemplate = 14
wdFormatXMLTemplateMacroEnabled = 15
wdFormatXPS = 18
照着字面意思应该能对应到相应的文件格式,如果你是office 2003可能支持不了这么多格式。word文件转html有两种格式可选wdFormatHTML、wdFormatFilteredHTML(对应数字 8、10),区别是如果是wdFormatHTML格式的话,word文件里面的公式等ole对象将会存储成wmf格式,而选用 wdFormatFilteredHTML的话公式图片将存储为gif格式,而且目测可以看出用wdFormatFilteredHTML生成的HTML 明显比wdFormatHTML要干净许多。
当然你也可以用任意一种语言通过com来调用office API,比如PHP.
from win32com import client as wc
word = wc.Dispatch('Word.Application')
doc = word.Documents.Open(r'c:/test1.doc')
doc.SaveAs('c:/test1.text', 4)
doc.Close()
import re
strings=open(r'c:\test1.text','r').read()
result=re.findall('\(\s*[A-D]\s*\)|\(\xa1*[A-D]\xa1*\)|\(\s*[A-D]\s*\)|\(\xa1*[A-D]\xa1*\)',strings)
chan=re.sub('\(\s*[A-D]\s*\)|\(\xa1*[A-D]\xa1*\)|\(\s*[A-D]\s*\)|\(\xa1*[A-D]\xa1*\)','()',strings)
question=open(r'c:\question','a+')
question.write(chan)
question.close()
answer=open(r'c:\answeronly','a+')
for i,a in enumerate(result):
m=re.search('[A-D]',a)
answer.write(str(i+1)+' '+m.group()+'\n')
answer.close()
chan=re.sub(r'\xa3\xa8\s*[A-D]\s*\xa3\xa9','()',strings)
#不要(),容易引起歧义。
希望本文所述对大家的Python程序设计有所帮助。
标签:python,word
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
新手入门Mysql--sql执行过程
2024-01-25 11:21:37
![](https://img.aspxhome.com/file/2023/9/124079_0s.jpg)
Python使用Phantomjs截屏网页的方法
2022-06-17 15:47:14
Python中import导入不同目录的模块方法详解
2021-04-08 02:37:08
什么是SVG(可升级矢量图形)
2008-05-06 12:37:00
ORACLE LATERAL-SQL-INJECTION 个人见解
2009-03-04 10:34:00
python实现WebSocket服务端过程解析
2022-09-14 10:45:19
![](https://img.aspxhome.com/file/2023/5/75855_0s.png)
PyTorch 如何自动计算梯度
2023-08-13 14:44:20
![](https://img.aspxhome.com/file/2023/7/59837_0s.jpg)
J2EE基础应用:J2EE中SQL语句自动构造方法
2009-09-18 09:06:00
JavaScript监听触摸事件代码实例
2023-08-20 19:12:54
asp 正则实现清除html文本格式的函数代码
2011-03-09 11:21:00
使用python进行文本预处理和提取特征的实例
2022-07-13 21:25:47
SQL Server双服务器架设并数据自动同步教程
2023-07-03 02:59:53
详细介绍ASP内置对象Response
2008-06-23 12:42:00
利用python实现冒泡排序算法实例代码
2021-06-29 17:00:14
![](https://img.aspxhome.com/file/2023/2/90522_0s.png)
分享10个有趣的Python程序
2023-12-21 19:53:58
![](https://img.aspxhome.com/file/2023/7/130647_0s.png)
python之线程池map()方法传递多参数list
2022-06-13 09:00:55
![](https://img.aspxhome.com/file/2023/6/131226_0s.jpg)
python技能之数据导出excel的实例代码
2022-09-21 15:01:53
![](https://img.aspxhome.com/file/2023/5/124745_0s.png)
解决每次打开pycharm直接进入项目的问题
2021-05-08 00:27:45
![](https://img.aspxhome.com/file/2023/2/115732_0s.jpg)
基于Opencv的图像卡通化实现代码
2023-07-10 06:58:55
![](https://img.aspxhome.com/file/2023/9/123779_0s.jpg)
python中pdb模块实例用法
2023-10-14 19:04:48