{"record":{"id":"daa460ed9e5e3def","repo":"NanmiCoder/MediaCrawler","slug":"openpyxl-is-required-for-excel-export-install-it","errorCode":null,"errorMessage":"openpyxl is required for Excel export. Install it with: pip install openpyxl","messagePattern":"openpyxl is required for Excel export\\. Install it with: pip install openpyxl","errorType":"exception","errorClass":"ImportError","httpStatus":null,"severity":"error","filePath":"store/excel_store_base.py","lineNumber":105,"sourceCode":"        with cls._lock:\n            for key, instance in cls._instances.items():\n                try:\n                    instance.flush()\n                    utils.logger.info(f\"[ExcelStoreBase] Flushed instance: {key}\")\n                except Exception as e:\n                    utils.logger.error(f\"[ExcelStoreBase] Error flushing {key}: {e}\")\n            cls._instances.clear()\n\n    def __init__(self, platform: str, crawler_type: str = \"search\"):\n        \"\"\"\n        Initialize Excel store\n\n        Args:\n            platform: Platform name (xhs, dy, ks, etc.)\n            crawler_type: Type of crawler (search, detail, creator)\n        \"\"\"\n        if not EXCEL_AVAILABLE:\n            raise ImportError(\n                \"openpyxl is required for Excel export. \"\n                \"Install it with: pip install openpyxl\"\n            )\n\n        super().__init__()\n        self.platform = platform\n        self.crawler_type = crawler_type\n\n        # Create data directory\n        if config.SAVE_DATA_PATH:\n            self.data_dir = Path(config.SAVE_DATA_PATH) / platform\n        else:\n            self.data_dir = Path(\"data\") / platform\n        self.data_dir.mkdir(parents=True, exist_ok=True)\n\n        # Initialize workbook\n        self.workbook = openpyxl.Workbook()\n        self.workbook.remove(self.workbook.active)  # Remove default sheet","sourceCodeStart":87,"sourceCodeEnd":123,"githubUrl":"https://github.com/NanmiCoder/MediaCrawler/blob/d6f7c5bb906b6dac40ddf343ef9e26438a3de092/store/excel_store_base.py#L87-L123","documentation":"Raised by ExcelStoreBase.__init__ when the openpyxl package is absent (EXCEL_AVAILABLE is False). Excel is treated as an optional dependency — importing store modules works, but instantiating any platform's excel store (SAVE_DATA_OPTION='excel') raises ImportError with install instructions.","triggerScenarios":"Running any crawler with SAVE_DATA_OPTION='excel' in an environment where 'pip install openpyxl' was never executed — including fresh clones, slim Docker images, and CI runners that only install base requirements.","commonSituations":"New deployment without the excel extra; a Dockerfile that skips optional deps; switching SAVE_DATA_OPTION to 'excel' on an existing install that never needed it before.","solutions":["pip install openpyxl (or add it to your requirements/lockfile)","If using the project's extras mechanism, install with the excel extra, e.g. pip install '.[excel]' where provided","In Docker/CI, add openpyxl to the image dependencies","Alternatively switch SAVE_DATA_OPTION to csv/json/sqlite which need no extra packages"],"exampleFix":"# before\nSAVE_DATA_OPTION = \"excel\"   # ImportError: openpyxl is required\n\n# after\npip install openpyxl\nSAVE_DATA_OPTION = \"excel\"   # now works","handlingStrategy":"validation","validationCode":"def excel_export_ready() -> bool:\n    try:\n        import openpyxl  # noqa: F401\n        return True\n    except ImportError:\n        return False\n\nif config.SAVE_DATA_OPTION == \"excel\" and not excel_export_ready():\n    raise SystemExit(\"install openpyxl first: pip install openpyxl\")","typeGuard":"def can_use_excel_store() -> bool:\n    import importlib.util\n    return importlib.util.find_spec(\"openpyxl\") is not None","tryCatchPattern":"try:\n    store = XhsExcelStoreImplement()\nexcept ImportError as e:\n    raise SystemExit(f\"missing optional dependency: {e}. Run: pip install openpyxl\") from e","preventionTips":["Add openpyxl to requirements/Docker image when SAVE_DATA_OPTION is 'excel'","Feature-check optional deps at startup and fail with an install hint","Fall back to csv export if Excel is not installable in the environment"],"tags":["dependency","excel","openpyxl","storage"],"backgroundTag":null,"analyzedSha":"d6f7c5bb906b6dac40ddf343ef9e26438a3de092","analyzedAt":"2026-08-15T01:39:07.505Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}