报错详情

原因
- 执行到
document=Document(word_path)
时报错,python-docx
模块无法读取该docx
文件
def open_file(word_path,excel_path,excel_sheet_name):
document=Document(word_path)
tables=document.tables
table_indexs=len(tables)
wb=openpyxl.load_workbook(excel_path)
sheet=wb[excel_sheet_name]
return tables,table_indexs,wb,sheet
解决办法
def doc_to_docx(path):
if os.path.splitext(path)[1] == ".doc":
word = client.Dispatch('Word.Application')
doc = word.Documents.Open(path)
print("原始数据路径:"+os.path.splitext(path)[0])
doc.SaveAs(os.path.splitext(path)[0]+".docx",16)
try:
print('保存文件')
doc.Close()
print('关闭Word.Application')
word.Quit()
except Exception as e:
print(e)
- 将
doc.SaveAs(os.path.splitext(path)[0]+".docx",16)
修改为如下代码
doc.SaveAs(os.path.splitext(path)[0]+".docx",12)
- 转换格式有问题导致
python-docx
读取docx
文件失败
疑问
- 在另外一台电脑上,
doc.SaveAs(os.path.splitext(path)[0]+".docx",16)
转换的格式未出现问题 - 在出问题的电脑上,使用
doc.SaveAs(os.path.splitext(path)[0]+".docx",16)
方式转换的docx
可以使用wps
正常打开