Skip to content

Instantly share code, notes, and snippets.

@hideaki-t
Last active March 16, 2017 14:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hideaki-t/1af95d03989e557056a8f71e4e399087 to your computer and use it in GitHub Desktop.
Save hideaki-t/1af95d03989e557056a8f71e4e399087 to your computer and use it in GitHub Desktop.
show dimension of an xlsx file
import zipfile
import xml.sax
import sys
NS = 'http://schemas.openxmlformats.org/spreadsheetml/2006/main'
class NonLocalExit(Exception):
pass
class H(xml.sax.handler.ContentHandler):
def startElementNS(self, name, qname, attrs):
if name == (NS, 'dimension'):
self.ref = attrs.get((None, 'ref'))
raise NonLocalExit('found')
parser = xml.sax.make_parser()
parser.setFeature(xml.sax.handler.feature_namespaces, True)
handler = H()
parser.setContentHandler(handler)
with zipfile.ZipFile(sys.argv[1]) as z:
with z.open('xl/worksheets/sheet1.xml') as f:
try:
parser.parse(f)
except NonLocalExit:
pass
print(handler.ref)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment