Python · Test Data
Generate Batches of Placeholder PDFs and Images for Upload Testing
Set a name, a count and an icon folder; the scripts produce numbered test documents (PDF via Word, or PNG/JPG via Pillow) for testing upload forms and workflows.
Pythonpython-docxPillowcomtypes
Abstract
The problem
Testing upload forms and document workflows needs dozens of distinct sample files, and creating them by hand is slow.
The solution
Two scripts generate N numbered files: one builds Word documents and converts them to PDF through Microsoft Word, the other draws PNG/JPG images with Pillow. Each shows a heading, date, time and a centred icon cycled from a folder.
The result
Any number of unique, clearly labelled test files in seconds.
Input & Output
Input
- An icon/image folder
- base_name, count and output folder set in the script
Output
- {base_name}_{n}.pdf (Word route) or .png/.jpg (image route)
Optional output
- Intermediate .docx files (delete line is commented out)
System Diagram
Icon folder
Sorted images, circular use
Build document
Heading · date · time · icon
Convert / draw
Word -> PDF or Pillow canvas
Numbered files
{base_name}_1 … _N
How It Works
Circular image selection
Icons are taken in sorted order and wrap around, starting from image_start_index.
Two output routes
Word-to-PDF via python-docx and Word automation, or PNG/JPG via Pillow.
Configurable layout
Heading size, image height, canvas size and formats are all parameters.
Timestamped content
Each file shows the generation date and time so test files are easy to tell apart.
Configuration
| Option | Purpose |
|---|---|
base_name / count | File name prefix and how many files to create. |
image_folder / output_dir | Where icons are read from and results are written. |
image_start_index | 1-based start position in the sorted icon list. |
output_formats | Image route only: ('png', 'jpg'). |
How to Run
pip install -r requirements.txtpython word_to_pdf_batch.pypython image_batch.pySource Code
import os
import sys
from datetime import datetime
from docx import Document
from docx.shared import Pt, Inches
from docx.enum.text import WD_ALIGN_PARAGRAPH
import comtypes.client
import logging
# Setup logging
logging.basicConfig(level=logging.INFO, format='%(levelname)s: %(message)s')
def create_batch_word_pdfs(
base_name,
count,
image_folder,
output_dir=".",
heading_font_size=24,
image_height_in_inches=2.0,
image_start_index=1 # 1-based index
):
"""
Generates 'count' PDFs with:
- Filename: {base_name}_{i}.pdf
- Heading: {base_name}_{i}
- Date/Time with labels
- Image from sorted list in image_folder (circular if needed)
Parameters:
base_name (str): e.g., "Sample-1_Document"
count (int): Number of PDFs to generate (e.g., 5)
image_folder (str): Folder containing images
output_dir (str): Output directory
image_start_index (int): 1-based start index in sorted image list
"""
try:
# Validate inputs
if count < 1:
raise ValueError("Count must be at least 1.")
if image_start_index < 1:
raise ValueError("image_start_index must be >= 1.")
output_dir = os.path.abspath(output_dir)
os.makedirs(output_dir, exist_ok=True)
# Get and sort images
if not os.path.isdir(image_folder):
raise FileNotFoundError(f"Image folder not found: {image_folder}")
valid_ext = ('.png', '.jpg', '.jpeg', '.bmp', '.gif', '.tiff')
image_files = sorted([
f for f in os.listdir(image_folder)
if f.lower().endswith(valid_ext)
])
if not image_files:
raise FileNotFoundError(f"No valid images found in: {image_folder}")
total_images = len(image_files)
logging.info(f"Found {total_images} images (sorted alphabetically).")… 88 more lines - loading the full file
Released under the MIT license. Free to use, modify and share.
Impact & Benefits
Frequently Asked Questions
How do I generate many PDF files with Python?
word_to_pdf_batch.py builds a .docx per file with python-docx and exports it to PDF through Word automation (Windows).
Can I make PNG or JPG test images instead?
Yes. image_batch.py draws the same layout with Pillow and saves PNG and/or JPG.
Is it meant for real documents?
No. It is for placeholder test data only.
Related
List Every File in a Folder to a Text or CSV Manifest with Python
Crawls a folder and produces a clean, copy-ready manifest of file names, types and sizes.
Read documentation