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

运行之后

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个回答

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

调用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();
    }
}
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐