A utility to generate test files in various formats from text content
Project description
Text2File
A powerful utility to generate test files in various formats from text content. Perfect for testing, development, and automation tasks.
Table of Contents
Features
- 🚀 Generate files in multiple formats with a single command
- 📄 Support for various file types:
- Text:
.txt,.md,.html,.css,.js,.py,.json,.csv - Documents:
.pdf,.docx,.odt - Spreadsheets:
.xlsx - Images:
.jpg,.jpeg,.png,.bmp,.gif - Archives:
.zip,.tar,.tar.gz,.tgz
- Text:
- ✅ File validation to ensure generated files are not corrupted
- 🔄 Clean up invalid files with a single command
- 📂 Customizable output directory and filename prefix
- 🔍 Recursive directory scanning for validation and cleanup
- 🛠️ Extensible architecture for adding new file formats
- 🧪 Built-in testing and validation
Installation
Using pip (recommended)
pip install text2file
From source
-
Clone the repository:
git clone https://github.com/veridock/text2file.git cd text2file
-
Install with Poetry (recommended):
poetry installOr with pip:
pip install .
Usage
Basic Syntax
text2file [COMMAND] [OPTIONS]
Available Commands
generate- Generate files in various formatsgenerate-set- Generate a set of images from a JSON configurationvalidate- Validate generated filescleanup- Clean up invalid files--version- Show version and exit--help- Show help message and exit
Examples
Generate a text file
text2file "Hello, World!" txt
text2file generate --content "Hello, World!" --extension txt
Generate a set of images from a JSON configuration
Create a JSON file (e.g., icons.json) with the following format:
{
"icons": [
{"src": "icon-16x16.png", "sizes": "16x16"},
{"src": "icon-32x32.png", "sizes": "32x32"},
{"src": "icon-64x64.png", "sizes": "64x64"}
]
}
Then run:
# Generate placeholder images with default text
text2file generate-set icons.json
# Use a base image and resize it
text2file generate-set icons.json --base-image source-icon.png
# Customize the placeholder appearance
text2file generate-set icons.json --text "My App" --background-color "#f0f0f0" --text-color "#333333"
Options:
-o, --output-dir: Output directory (default: current directory)-b, --base-image: Base image to use for resizing (optional)--bg, --background-color: Background color for placeholders (default: "#ffffff")-t, --text: Text to render on placeholder images--fg, --text-color: Text color for placeholders (default: "#000000")
Generate multiple files with different formats
text2file generate --content "Sample content" --extension txt,md,html --output-dir ./output
Validate generated files
text2file validate --path ./output --recursive --verbose
Clean up invalid files
text2file cleanup --path ./output --recursive --dry-run
File Format Examples
Text Files
Plain Text (.txt)
text2file generate --content "This is a plain text file" --extension txt
Markdown (.md)
content="# Sample Markdown\n\n- Item 1\n- Item 2\n- Item 3"
text2file generate --content "$content" --extension md
Code Files
Python (.py)
content="def hello_world():\n print('Hello, World!')\n\nif __name__ == '__main__':\n hello_world()"
text2file generate --content "$content" --extension py
JavaScript (.js)
content="function helloWorld() {\n console.log('Hello, World!');\n}\n\nhelloWorld();"
text2file generate --content "$content" --extension js
Documents
PDF (.pdf)
text2file generate --content "This is a sample PDF document" --extension pdf
Word Document (.docx)
text2file generate --content "This is a sample Word document" --extension docx
Spreadsheets
Excel (.xlsx)
content="Name,Age,City\nJohn,30,New York\nAlice,25,London"
text2file generate --content "$content" --extension xlsx
Image Sets
Generate multiple images from a JSON configuration file. This is useful for creating icon sets or multiple resolutions of the same image.
JSON Configuration Format
Create a file (e.g., icons.json) with the following structure:
{
"icons": [
{
"src": "icon-16x16.png",
"sizes": "16x16"
},
{
"src": "icon-32x32.png",
"sizes": "32x32"
},
{
"src": "subdir/icon-64x64.png",
"sizes": "64x64"
}
]
}
Each entry in the icons array should have:
src: Output file path (relative to output directory)sizes: Dimensions in format "WIDTHxHEIGHT" (e.g., "32x32")
Usage Examples
# Generate placeholder images with default text
text2file generate-set icons.json
# Use a base image and resize it
text2file generate-set icons.json --base-image source-icon.png
# Customize the placeholder appearance
text2file generate-set icons.json --text "My App" --background-color "#f0f0f0" --text-color "#333333"
# Specify output directory
text2file generate-set icons.json --output-dir ./output
Options
-o, --output-dir: Output directory (default: current directory)-b, --base-image: Base image to use for resizing (optional)--bg, --background-color: Background color for placeholders (default: "#ffffff")-t, --text: Text to render on placeholder images--fg, --text-color: Text color for placeholders (default: "#000000")
Single Images
JPEG (.jpg, .jpeg)
text2file generate --content "Sample image content" --extension jpg --width 800 --height 600
PNG (.png)
text2file generate --content "Sample image content" --extension png --width 800 --height 600
Archives
ZIP (.zip)
text2file generate --content "Sample content for archive" --extension zip
TAR.GZ (.tar.gz)
text2file generate --content "Sample content for tar.gz archive" --extension tar.gz
API Reference
text2file.generate_file(content: str, extension: str, output_dir: str = ".", filename: Optional[str] = None) -> str
Generate a file with the given content and extension.
Parameters
content: The content to write to the fileextension: The file extension (without dot)output_dir: Output directory (default: current directory)filename: Optional custom filename (without extension)
Returns
- Path to the generated file
text2file.validate_file(filepath: str) -> bool
Validate if a file is not corrupted.
Parameters
filepath: Path to the file to validate
Returns
Trueif file is valid,Falseotherwise
text2file.cleanup_invalid_files(directory: str, recursive: bool = False, dry_run: bool = False) -> List[str]
Remove invalid files from a directory.
Parameters
directory: Directory to clean uprecursive: Whether to scan subdirectoriesdry_run: If True, only show what would be deleted
Returns
- List of removed files
Development
Prerequisites
- Python 3.8+
- Poetry
Setup
-
Clone the repository:
git clone https://github.com/veridock/text2file.git cd text2file
-
Install dependencies:
poetry install -
Activate the virtual environment:
poetry shell
Running Tests
pytest
Linting and Formatting
# Run linter
ruff check .
# Format code
black .
# Check types
mypy .
Contributing
Contributions are welcome! Please read our Contributing Guidelines for details on how to contribute to this project.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Commands
Generate Files
text2file generate "Your text content" [EXTENSIONS...] [OPTIONS]
Options:
-o, --output-dir PATH: Output directory (default: current directory)-p, --prefix TEXT: Filename prefix (default: "generated")--help: Show help message and exit
Examples:
-
Generate a simple text file:
text2file generate "This is a test file" txt
-
Generate multiple file formats at once:
text2file generate "Sample content" txt md pdf jpg
-
Custom output directory and prefix:
text2file generate "Test content" -o ./output -p custom_ txt pdf
Validate Files
text2file validate PATH [OPTIONS]
Options:
-r, --recursive: Recursively validate files in subdirectories-v, --verbose: Show detailed validation results--json: Output results in JSON format--help: Show help message and exit
Examples:
-
Validate a single file:
text2file validate document.pdf
-
Validate all files in a directory recursively:
text2file validate ./test_files -r
-
Get detailed validation output in JSON:
text2file validate ./test_files --json
Clean Up Invalid Files
text2file cleanup PATH [OPTIONS]
Options:
-r, --recursive: Recursively clean up files in subdirectories--dry-run: Show what would be deleted without actually deleting-v, --verbose: Show detailed information--help: Show help message and exit
Examples:
-
Clean up invalid files in a directory:
text2file cleanup ./test_files
-
Preview what would be deleted (dry run):
text2file cleanup ./test_files --dry-run -v
File Format Examples
Text Files (.txt, .md)
text2file generate "# Markdown Title\n\nThis is a **markdown** file with *formatting*.\n\n- Item 1\n- Item 2\n- Item 3" md
Code Files (.py, .js, .html)
text2file generate "def hello_world():\n print('Hello, World!')" py
Images (.jpg, .png)
text2file generate "Sample text to be converted to image" jpg png
Documents (.pdf, .docx)
text2file generate "Formal Document\n\nThis is a sample document with multiple paragraphs. The text will be automatically wrapped to fit the output format. Great for creating test data!" pdf docx
Development
Setup
-
Install development dependencies:
poetry install --with dev
-
Run tests:
poetry run pytest
-
Run linter:
poetry run ruff check .
-
Run formatter:
poetry run black .
Adding New File Formats
- Create a new generator function in
src/text2file/generators/ - Add validation logic in
src/text2file/validators.pyif needed - Register the generator in
src/text2file/__init__.py - Add tests in
tests/ - Update documentation
Contributing
Contributions are welcome! Please read our Contributing Guidelines for details.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Changelog
See CHANGELOG.md for a history of changes.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file text2file-0.1.3.tar.gz.
File metadata
- Download URL: text2file-0.1.3.tar.gz
- Upload date:
- Size: 52.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.1.3 CPython/3.11.12 Linux/6.15.3-200.fc42.x86_64
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
355f533ba19901c0a1e102e4a1c97f20ba46f5c66e098e0c331befc62b464925
|
|
| MD5 |
a6770d2fee3e38326bf41ed8b996a9ec
|
|
| BLAKE2b-256 |
7937742739b3925c16fa281529db14a92bd1a31bb84bffc5c0bc210290ba3035
|
File details
Details for the file text2file-0.1.3-py3-none-any.whl.
File metadata
- Download URL: text2file-0.1.3-py3-none-any.whl
- Upload date:
- Size: 65.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.1.3 CPython/3.11.12 Linux/6.15.3-200.fc42.x86_64
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b8c7bd819e73022059a18e899a1fc1d74c189ebf07208b02e706722f4c7cc5b0
|
|
| MD5 |
385c2b1d995a8302263ca5b6fa41e7e4
|
|
| BLAKE2b-256 |
bb1ceea2b9247418d7c0c7b024c175a8ad730d31b87784ec04ee912db0b8d987
|