V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
tedd
V2EX  ›  Python

求教无所不能的 Python 大神,如何读取指定区域的 MS WORD 的内容?

  •  
  •   tedd · 2013-03-01 22:46:06 +08:00 · 7413 次点击
    这是一个创建于 4079 天前的主题,其中的信息可能已经有所发展或是发生改变。


    目标框是需要读取的内容,能将其读取出来就算任务完成了!搜了半天都没有找到合适的方案
    9 条回复    1970-01-01 08:00:00 +08:00
    min
        1
    min  
       2013-03-01 22:48:17 +08:00
    这个问题用ironpythn会不会简单点。。。
    phuslu
        2
    phuslu  
       2013-03-01 22:59:46 +08:00
    之前搞过, 用 win32com.client 搞.
    参考这个 c# 例程, 翻译一下.
    dreampuf
        4
    dreampuf  
       2013-03-02 02:14:33 +08:00
    最近在弄和office相关的,我最后选的方案是JPype call Apache POI
    gamexg
        5
    gamexg  
       2013-03-04 20:13:59 +08:00   ❤️ 1
    以前转换报表格式弄过word文档。


    import win32com
    from datetime import datetime
    from win32com.client import Dispatch, constants

    w = win32com.client.Dispatch('Word.Application')
    ts = w.Documents.Open(r'E:\n.doc').Tables


    print ts[0].Cell(4,4).Range.Text

    网上有vb的word参考文档,可以参考一下。
    hewigovens
        6
    hewigovens  
       2013-03-04 20:33:48 +08:00
    @phuslu 哈, 老本行啊, 我以前也用C++这么搞过
    tedd
        7
    tedd  
    OP
       2013-03-04 21:03:36 +08:00
    @gamexg

    太感谢了,我照着敲下来出现了这个报错:

    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    File "<COMObject <unknown>>", line 3, in Cell
    pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, u'Microsoft Word'
    , u'The requested member of the collection does not exist.', u'C:\\Program Files
    (x86)\\Microsoft Office\\Office12\\1033\\WDMAIN11.CHM', 25421, -2146822347), None)
    >>>
    gamexg
        8
    gamexg  
       2013-03-05 19:20:45 +08:00   ❤️ 1
    @tedd

    我这里没环境,看错误像是没有指定的单元格,Cell(4,4)指的是第四行第四列的单元格。

    详细信息可以从网上找一下 Microsoft+Word+Visual+Basic+参考.chm
    wzzyj8
        9
    wzzyj8  
       2013-03-05 20:51:05 +08:00
    VBA导出CSV,Python处理CSV。会不会快一点?毕竟对于Office来说自带的VBA读取起来会好一点
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   780 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 21:11 · PVG 05:11 · LAX 14:11 · JFK 17:11
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.