This Python project generates a customizable word search puzzle using words from the WordNet lexical database. The resulting word search is output as a PDF file, making it easy to print and enjoy. The word search puzzle can be tailored in terms of grid size, the number of words, and the title displayed at the top of the PDF. Some example output is included.
- Wordsearch Python
This project was inspired by a request on the PythonLearning subreddit by the user Mysterious_Lab5934.
- WordNet via NLTK: The word list is sourced from WordNet, a large lexical database of English, provided by the Natural Language Toolkit (NLTK). WordNet was chosen for its extensive coverage of English words, allowing for a rich and varied word search experience. Phrases (words containing underscores) and hyphenated words are automatically excluded to ensure only single, unhyphenated words are used.
- Word filtering: We attempt to filter out words that are considered offensive but cannot guarantee that this will be effective or correct in all cases.
- Grid Size: The default grid size is set to 25x25, providing a challenging and engaging puzzle size.
- Word Placement: Words can be placed in any direction: horizontally, vertically, or diagonally, in both forward and backward orientations, ensuring a high level of difficulty.
- Word List Display: The word list is displayed in three columns at the bottom of the PDF, with a smaller font size to fit neatly within the available space.
- Dynamic Filename: The output PDF file is named with a timestamp (
wordsearch_YYYYMMDD_HHMMSS.pdf
) to avoid overwriting previous files. - Output Directory: By default, PDFs are saved to a
wordsearch
directory. This directory can be customized by modifying theoutput_dir
variable in the script. - Title Customization: The title of the word search puzzle can be customized by modifying a variable in the script.
- Cell Size: The size of each cell in the word search grid can be automatically calculated to fit the grid width within the page. Alternatively, it can be set manually by adjusting the
cell_size
variable in the script.
- Python 3.6 or greater: Ensure you have Python installed on your system.
- pip: Python's package installer, typically included with Python.
This project uses the following Python packages:
nltk
: For accessing the WordNet word list.reportlab
: For generating the PDF output.better_profanity
: For screening out offensive words
All the necessary packages are listed in the requirements.txt
file. To install them, use the following command:
pip install -r requirements.txt
This will install the packages required to run the script successfully.
The first time you run the script, it will download the WordNet data if it is not already present. This data will be cached locally for subsequent runs.
-
Open the Command Prompt.
-
Navigate to the directory containing the script using the
cd
command. -
Run the script with Python:
python main.py
-
Open Terminal.
-
Navigate to the directory containing the script using the
cd
command. -
Run the script with Python:
python3 main.py
-
Open Terminal.
-
Navigate to the directory containing the script using the
cd
command. -
Run the script with Python:
python3 main.py
To change the title of the word search, modify the title
variable in the script:
title = "Your Custom Title"
You can adjust the grid size and the number of words by changing the following variables:
default_grid_size = 25 # Change this to your preferred grid size
default_num_words = 20 # Change this to set how many words are in the puzzle
The PDF files are saved by default in a wordsearch
directory. To change this directory, modify the output_dir
variable:
output_dir = "your_custom_directory"
The cell_size
variable in the script controls the size of each individual cell in the word search grid when it is rendered on the PDF. Specifically, it determines:
-
Letter Spacing: The amount of space (in points) that each letter occupies on the grid. A larger
cell_size
will space the letters farther apart, while a smallercell_size
will place them closer together. This affects both the readability of the letters and how much of the page the grid will fill. -
Grid Dimensions on the Page: The overall physical dimensions of the grid on the page. If you increase the
cell_size
, the grid will take up more space on the page, and if you decrease it, the grid will be more compact. For example, a largecell_size
might be useful if you want to create a word search with large, easily readable letters. -
Automatic Calculation: If the
cell_size
is set toNone
(which is the default), the script will automatically calculate an appropriatecell_size
that allows the entire grid to fit neatly within the available width of the A5 page. This ensures that the grid is fully displayed, regardless of the number of rows or columns. -
Manual Adjustment: You can manually set the
cell_size
if you want to control exactly how large or small the cells should be. For example:cell_size = 12 # Sets each cell to be 12 points wide and high
-
Small
cell_size
(e.g., 8 points):- The letters will be placed close together.
- The grid will appear more compact on the page.
- This is useful if you want to fit a larger grid on a small page.
-
Large
cell_size
(e.g., 15 points):- The letters will be spaced further apart.
- The grid will occupy more space on the page.
- This might be necessary if you're creating a word search for younger children or for someone who might prefer larger text.
By adjusting the cell_size
, you can control the appearance and layout of the word search grid to best suit your needs.
By default, the script filters words to include only those with a minimum length of 4 characters, excluding phrases and hyphenated words. You can adjust this filter in the script:
filtered_words = [word for word in sorted(list(words_set)) if len(word) >= 4 and "_" not in word and "-" not in word]
Special thanks to the user Mysterious_Lab5934
for their inquiry and the PythonLearning subreddit for providing a platform for learning and collaboration.