Python:3.7,平台:Win 10 64位
我想从MS Word文件中提取表格。我正在尝试使用以下代码在python中读取文件:
import win32com.client as win32
def parseWordFile(SigLexiconFilePath):
print('parsing Word File')
SigLexiconFilePath = "r\'" + SigLexiconFilePath
commands = []
word = win32.Dispatch("Word.Application")
word.Visible = 0
doc = word.Documents.Open(SigLexiconFilePath)
table = doc.Tables(1)
代码似乎很明显,但是没有用。我正在获取doc变量,因为None它导致以下错误。
Exception has occurred: AttributeError
'NoneType' object has no attribute 'Tables
我在这里做错了什么?
PS:我发现doc = word.ActiveDocument如果MS Word程序中已打开文件,则添加行会打开该文件。但是我想打开单词文档,即使它还没有打开。
感谢您指出文件路径中的错误。我试图以错误的方式将Windows的正斜杠路径转换为反斜杠路径。下面的代码修复了我的文件路径问题。
import win32com.client as win32
from pathlib import Path
def parseWordFile(SigLexiconFilePath):
print('parsing Word File')
SigLexiconFilePath = Path(SigLexiconFilePath)
commands = []
word = win32.Dispatch("Word.Application")
word.Visible = 0
doc = word.Documents.Open(SigLexiconFilePath._str)
table = doc.Tables(1)
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。