weixin_42489752
王喜兴喝橙汁
采纳率0%
2019-08-02 23:31

python下怎么读取doc 或者把doc转换成docx

  • python

运行之后

import sys
import pickle
import re
import  codecs
import string
import shutil
from win32com import client as wc
import docx


def doSaveAas():
    word = wc.Dispatch('Word.Application')
    doc = word.Documents.Open(u'E:\\3\\减肥日记-副本.doc')        # 目标路径下的文件
    doc.SaveAs(u'E:\\code\\hhhhhhhh.docx', 16, False, "", True, "", False, False, False, False)  # 转化后路径下的文件    
    doc.Close()
    word.Quit()

doSaveAas()

Traceback (most recent call last):
File "c:\Users\Administrator.vscode\extensions\ms-python.python-2019.6.24221\pythonFiles\ptvsd_launcher.py", line 43, in
main(ptvsdArgs)
File "c:\Users\Administrator.vscode\extensions\ms-python.python-2019.6.24221\pythonFiles\lib\python\ptvsd__main__.py", line 434, in main
run()
File "c:\Users\Administrator.vscode\extensions\ms-python.python-2019.6.24221\pythonFiles\lib\python\ptvsd__main__.py", line 312, in run_file
runpy.run_path(target, run_name='__main__')
File "D:\Users\Administrator\Anaconda3\lib\runpy.py", line 263, in run_path
pkg_name=pkg_name, script_name=fname)
File "D:\Users\Administrator\Anaconda3\lib\runpy.py", line 96, in run_module_code
mod_name, mod_spec, pkg_name, script_name)
File "D:\Users\Administrator\Anaconda3\lib\runpy.py", line 85, in _run_code
exec(code, run_globals)
File "d:\Untitled-1.py", line 18, in
doSaveAas()
File "d:\Untitled-1.py", line 12, in doSaveAas
word = wc.Dispatch('Word.Application')
File "D:\Users\Administrator\Anaconda3\lib\site-packages\win32com\client\
_init__.py", line 95, in Dispatch
dispatch, userName = dynamic._GetGoodDispatchAndUserName(dispatch,userName,clsctx)
File "D:\Users\Administrator\Anaconda3\lib\site-packages\win32com\client\dynamic.py", line 114, in _GetGoodDispatchAndUserName
return (_GetGoodDispatch(IDispatch, clsctx), userName)
File "D:\Users\Administrator\Anaconda3\lib\site-packages\win32com\client\dynamic.py", line 91, in _GetGoodDispatch
IDispatch = pythoncom.CoCreateInstance(IDispatch, None, clsctx, pythoncom.IID_IDispatch)
pywintypes.com_error: (-2147221005, '无效的类字符串', None, None)

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答

  • Miaonly Dadamia 2年前

    调用java api free spire.doc for java
    ,可不用安装office,直接保存为成docx就行

    import com.spire.doc.*;
    
    public class DocToDocx {
         public static void main(String[]args) {
            Document doc = new Document();
            doc.loadFromFile("test.doc", FileFormat.Doc);
            doc.saveToFile("ToDocx.docx",FileFormat.Docx_2013);
            doc.dispose();
        }
    }
    
    点赞 评论 复制链接分享
  • caozhy 从今以后生命中的每一秒都属于我爱的人 2年前

    你采用调用vba的方法,你的计算机上正确安装office了么

    点赞 评论 复制链接分享

相关推荐