使用docx.Document()时的回溯(最近一次调用)

时间:2018-09-16 14:51:36

标签: python python-3.x docx python-docx

import docx

f = open('~/Desktop/python/test/draft.docx','rb')

document = docx.Document(f)

Traceback (most recent call last):
  File "./test.py", line 56, in <module>
    document = docx.Document(f) 
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/docx/api.py", line 25, in Document
    document_part = Package.open(docx).main_document_part
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/docx/opc/package.py", line 116, in open
    pkg_reader = PackageReader.from_file(pkg_file)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/docx/opc/pkgreader.py", line 32, in from_file
    phys_reader = PhysPkgReader(pkg_file)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/docx/opc/phys_pkg.py", line 101, in __init__
    self._zipf = ZipFile(pkg_file, 'r')
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/zipfile.py", line 1200, in __init__
    self._RealGetContents()
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/zipfile.py", line 1267, in _RealGetContents
    raise BadZipFile("File is not a zip file")
zipfile.BadZipFile: File is not a zip file
  1. 已卸载docx和已安装python-docx
  2. 也卸载了lxml 没有一个有效。

任何帮助将不胜感激 在OS X10.13上运行python3.7

1 个答案:

答案 0 :(得分:0)

在将文件传递给add-type -AssemblyName microsoft.VisualBasic add-type -AssemblyName System.Windows.Forms Notepad start-sleep -Milliseconds 500 #[Microsoft.VisualBasic.Interaction]::AppActivate("GUI_APP_I_USE") [Microsoft.VisualBasic.Interaction]::AppActivate("Notepad") [System.Windows.Forms.SendKeys]::SendWait("Hello") #[Some.Library.I.Drag.Drop.With]::DragStart(x1,y1) #[Some.Library.I.Drag.Drop.With]::DragEnd(x2,y2) 之前不要打开文件。就像在上面的Document()调用中那样给它提供路径。

它必须是实际的Word .docx文件。请注意,您只需致电open()即可开始使用。 document = Document()调用中提供了“另存为”文件名。 document.save()调用中提供的文件(如果有)只是要使用的起点“模板”。

在此处查看相关文档:
https://python-docx.readthedocs.io/en/latest/user/documents.html