"""Minimal .xlsx reader (zipfile + ElementTree), standard library only. Data only. Fact-Checked Science ("Our analysis"), https://factchecked.science/planthopper-magnetic-genes/ Data: Xie, W., et al. (2026), Dryad, doi:10.5061/dryad.r4xgxd2vx (CC0); make the input folder with prepare_inputs.py, then pass /dataset (or /field for igrf_route.py) as the script's usage says. read_xlsx(path) -> {sheet_name: {"dimension": "A1:G100", "rows": [[cell, ...], ...], "raw": [[str|None, ...]]}} Cells keep the value stored in the file: numbers as float (and their stored text in "raw"), strings as str, booleans as bool, errors as "#ERR:", empty cells as None. Column positions come from cell references, so gaps stay gaps. Formulas: the cached value is used (the formula itself is ignored). Usage as a script: python3 xlsx_reader.py [max_rows] (prints a preview of every sheet) """ import re import sys import zipfile import xml.etree.ElementTree as ET NS = {"m": "http://schemas.openxmlformats.org/spreadsheetml/2006/main", "r": "http://schemas.openxmlformats.org/officeDocument/2006/relationships", "pr": "http://schemas.openxmlformats.org/package/2006/relationships"} REF = re.compile(r"^([A-Z]+)(\d+)$") def col_index(letters): n = 0 for ch in letters: n = n * 26 + (ord(ch) - 64) return n - 1 def _text(el): return "".join(el.itertext()) if el is not None else "" def read_xlsx(path): z = zipfile.ZipFile(path) names = set(z.namelist()) shared = [] if "xl/sharedStrings.xml" in names: root = ET.fromstring(z.read("xl/sharedStrings.xml")) for si in root.findall("m:si", NS): # concatenate all text runs, skipping phonetic runs parts = [] for el in si.iter(): tag = el.tag.split("}")[-1] if tag == "t": parts.append(el.text or "") if tag == "rPh": break shared.append("".join(parts)) wb = ET.fromstring(z.read("xl/workbook.xml")) rels = ET.fromstring(z.read("xl/_rels/workbook.xml.rels")) target = {r.get("Id"): r.get("Target") for r in rels.findall("pr:Relationship", NS)} out = {} for sh in wb.find("m:sheets", NS).findall("m:sheet", NS): name = sh.get("name") rid = sh.get("{%s}id" % NS["r"]) t = target[rid].lstrip("/") if not t.startswith("xl/"): t = "xl/" + t root = ET.fromstring(z.read(t)) dim = root.find("m:dimension", NS) rows, raw = [], [] for row in root.find("m:sheetData", NS).findall("m:row", NS): rnum = int(row.get("r")) - 1 while len(rows) < rnum: rows.append([]) raw.append([]) vals, rvals = [], [] for c in row.findall("m:c", NS): m = REF.match(c.get("r")) ci = col_index(m.group(1)) while len(vals) < ci: vals.append(None) rvals.append(None) typ = c.get("t", "n") v = c.find("m:v", NS) if typ == "s": s = shared[int(v.text)] vals.append(s) rvals.append(s) elif typ == "inlineStr": s = _text(c.find("m:is", NS)) vals.append(s) rvals.append(s) elif typ == "str": s = v.text if v is not None else "" vals.append(s) rvals.append(s) elif typ == "b": vals.append(v is not None and v.text == "1") rvals.append(v.text if v is not None else None) elif typ == "e": vals.append("#ERR:" + (v.text if v is not None else "")) rvals.append(v.text if v is not None else None) else: if v is None or v.text is None: vals.append(None) rvals.append(None) else: vals.append(float(v.text)) rvals.append(v.text) rows.append(vals) raw.append(rvals) out[name] = {"dimension": dim.get("ref") if dim is not None else None, "rows": rows, "raw": raw} return out def main(): path = sys.argv[1] lim = int(sys.argv[2]) if len(sys.argv) > 2 else 8 d = read_xlsx(path) for name, s in d.items(): ncol = max((len(r) for r in s["rows"]), default=0) print("== sheet %r | dimension %s | rows read %d | max columns %d" % (name, s["dimension"], len(s["rows"]), ncol)) for r in s["raw"][:lim]: sys.stdout.buffer.write((repr(r)[:400] + "\n").encode("utf-8")) if __name__ == "__main__": main()