Skip to content

"not a zip file" in Python 3 #765

@pfuntner

Description

@pfuntner

This is more of a question but I don't know where to ask questions.

I have this collection of tools that worked well in Python 2 and one of them used python-docx to look at *.docx files. I'm switching my tools to use Python 3 and found that a script that works with Python 2 does not work with Python 3:

$ cat docx
#! /usr/bin/env python3

import os
import sys
from docx import Document # from python-docx

assert len(sys.argv) == 2, 'Specify a filename'
assert os.path.isfile(sys.argv[1]), 'Cannot find {}'.format(sys.arg[1])

print('Python {}'.format(sys.version_info))
with open(sys.argv[1]) as stream:
  doc = Document(stream)
  print('{} paragraphs'.format(len(doc.paragraphs)))
$ ./docx Hello.docx
Python sys.version_info(major=3, minor=6, micro=9, releaselevel='final', serial=0)
Traceback (most recent call last):
  File "./docx", line 31, in <module>
    doc = Document(stream)
  File "/usr/lib/python3.6/site-packages/docx/api.py", line 25, in Document
    document_part = Package.open(docx).main_document_part
  File "/usr/lib/python3.6/site-packages/docx/opc/package.py", line 128, in open
    pkg_reader = PackageReader.from_file(pkg_file)
  File "/usr/lib/python3.6/site-packages/docx/opc/pkgreader.py", line 32, in from_file
    phys_reader = PhysPkgReader(pkg_file)
  File "/usr/lib/python3.6/site-packages/docx/opc/phys_pkg.py", line 101, in __init__
    self._zipf = ZipFile(pkg_file, 'r')
  File "/usr/lib/python3.6/zipfile.py", line 1131, in __init__
    self._RealGetContents()
  File "/usr/lib/python3.6/zipfile.py", line 1198, in _RealGetContents
    raise BadZipFile("File is not a zip file")
zipfile.BadZipFile: File is not a zip file
$ python2 ./docx Hello.docx
Python sys.version_info(major=2, minor=7, micro=16, releaselevel='final', serial=0)
1 paragraphs
$ pip3 list | grep docx
python-docx 0.8.10
$ 

I see the documentation says is supports Python 3.3 and 3.4. Are there plans to support 3.6?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions