好友
阅读权限 10
听众
最后登录 1970-1-1
想用python把word中得书签读取上来,然后用测试的结果代替掉,结果现在连word都没读上来。求大牛指点。还有后续的将word打印成pdf的部分有方法的也可以告知一下,谢谢
word文件好像无法上传?这咋搞
现在是用win32com然后就报出
Traceback (most recent call last):
File "D:\python\Main.py", line 2, in <module>
app = client.Dispatch(r'D:\python\test.docx') # 启动独立进程
File "E:\python\lib\site-packages\win32com\client\__init__.py", line 95, in Dispatch
dispatch, userName = dynamic._GetGoodDispatchAndUserName(dispatch,userName,clsctx)
File "E:\python\lib\site-packages\win32com\client\dynamic.py", line 98, in _GetGoodDispatchAndUserName
return (_GetGoodDispatch(IDispatch, clsctx), userName)
File "E:\python\lib\site-packages\win32com\client\dynamic.py", line 83, in _GetGoodDispatch
IDispatch = pythoncom.CoCreateInstance(IDispatch, None, clsctx, pythoncom.IID_IDispatch)
pywintypes.com_error: (-2147221005, '无效的类字符串', None, None)
from win32com import client
app = client.Dispatch(r'D:\python\test.docx' ) # 启动独立进程
app.Visible = True #// 设置是否可见 , 如果为 false, 则后台运行
word = app.Documents.Open('Word 文档路径 ' ) # 打开 word 文档
bookmarks = word.BookMarks # 获取所有书签
用docx就报出在 'docx.py' 中找不到引用 'document'
from docx import document
# 读取全文本
document = Document(r'D:\python\test.docx' )
all_paragraphs = document.paragraphs
for paragraph in all_paragraphs:
print (paragraph.text)
# 读取表格中的文字
document = Document(r'D:\python\test.docx' )
all_tables = document.tables
for table in all_tables:
for row in table.rows:
for cell in row.cells:
print (cell.text)
发帖前要善用【论坛搜索 】 功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。