Author: Mehedy Hasan
Organisation: ARCED Foundation
Position: Research Assistant
A lightweight Python tool that automatically generates a codebook (Excel file) from survey datasets. Supports both Stata .dta files and Excel .xlsx/.xls files.
The generated codebook contains five columns:
| Column | Description |
|---|---|
| Variable | The variable name in the dataset |
| Obs | Number of non-missing observations |
| Miss obs | Number of missing observations |
| Ques. | The survey question text (variable label) |
| Label | The variable label/description |
- Works on Google Colab — upload and run, no installation needed.
- Also works on local Jupyter notebooks.
- Handles Stata files with variable labels via
pyreadstat. - Handles Excel files via
pandas. - Auto-downloads the output Excel file on Colab.
- Open the notebook in Colab: click Open in Colab badge below or upload the
.ipynbfile manually. - Click Runtime > Run all (or press
Ctrl+F9). - In the Upload cell, click Choose Files and select your
.dtaor.xlsxfile. - The codebook is generated and downloaded automatically as
codebook.xlsx.
-
Clone this repository:
git clone https://github.com/mehedy0905/codebook-generator.git cd codebook-generator -
Install dependencies:
pip install pandas openpyxl pyreadstat
-
Launch Jupyter:
jupyter notebook codebook_generator.ipynb
-
Run all cells (
Kernel > Restart & Run All). The upload cell will prompt you to select a file.
In Step 4 of the notebook, you can customise:
| Parameter | Default | Options | Description |
|---|---|---|---|
OUTPUT_NAME |
"codebook.xlsx" |
any string | Name of the output Excel file |
QUES_SOURCE |
"label" |
"label" or "none" |
Controls the Ques. column content |
"label"— Uses the variable label as the question text (recommended for surveys)."none"— Leaves the Ques. column blank.
codebook-generator/
├── codebook_generator.ipynb # Main notebook
├── README.md # This file
├── .gitignore # Git ignore rules
└── LICENSE # MIT License
- Python 3.8+
pandasopenpyxlpyreadstat
All dependencies are auto-installed by the notebook if missing.
This project is licensed under the MIT License. See the LICENSE file for details.
Mehedy Hasan
Research Assistant, ARCED Foundation