Last active
March 16, 2017 14:43
-
-
Save hideaki-t/1af95d03989e557056a8f71e4e399087 to your computer and use it in GitHub Desktop.
show dimension of an xlsx file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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