Skip to main content

Convert source code into beautiful syntax-highlighted and auto-cropped images

Project description

PySnapCode

PySnapCode is a lightweight Python library designed to transform your source code into beautiful, syntax-highlighted, and auto-cropped images. It is the perfect tool for sharing code snippets on social media, technical blogs, or project documentation.


Prerequisites (Mandatory)

To ensure this library functions correctly, you must install the following two external dependencies.

Installation Guide

If you need help setting up these dependencies, follow this step-by-step video tutorial: View Tutorial on YouTube

1. Poppler (Required for Image Generation)

  • Windows: Download the latest version from the Poppler for Windows Releases.
  • Setup: Extract the ZIP file and copy the path to the bin folder. You will need to provide this path in your Python script.

2. wkhtmltopdf (Required for PDF Rendering)

  • Download: Get the installer from the official wkhtmltopdf website.
  • Setup: Run the installer. It is highly recommended to add it to your system PATH during the installation process.

Installation

You can install PySnapCode directly from PyPI using pip:

pip install PySnapCode


### Usage
Basic Example
This example shows how to convert a Python file using the default dark theme.

```python
from py_snap_code import CodeConverter

# Initialize the converter with your Poppler bin path
conv = CodeConverter(poppler_path=r'C:\path\to\your\poppler\bin')

# Convert your python file to a cropped image
conv.convert('main.py', output_name='my_code_snap')

Advanced Customization

You can fully customize the colors and font size to match your personal style or brand.

from py_snap_code import CodeConverter

# Define a custom theme (e.g., Dracula-inspired)
my_theme = {
    'bg': '#282a36',        # Background color
    'text': '#f8f8f2',      # Primary text color
    'keywords': '#ff79c6',  # Python keywords
    'numbers': '#bd93f9',   # Numbers
    'line_num': '#6272a4'   # Line numbers color
}

conv = CodeConverter(
    poppler_path=r'C:\poppler\bin',
    custom_colors=my_theme,
    font_size="18px"
)

conv.convert('script.py', output_name='pro_snippet')

Key Features

Auto-Crop: Automatically detects the code area and trims excess white space.

Syntax Highlighting: Built-in support for standard Python keywords and operators.

Theming Engine: Full control over background, text, and highlighting color palettes.

Professional Padding: Adds clean, consistent padding around your code for a better visual look.


License

This project is licensed under the MIT License - see the LICENSE file for details.


Support & Donations

If you find this library helpful and want to support its development, you can buy me a coffee here:

Support me on Ko-fi

Donate via Ko-fi

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pysnapcode-0.1.1.tar.gz (6.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pysnapcode-0.1.1-py3-none-any.whl (6.2 kB view details)

Uploaded Python 3

File details

Details for the file pysnapcode-0.1.1.tar.gz.

File metadata

  • Download URL: pysnapcode-0.1.1.tar.gz
  • Upload date:
  • Size: 6.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.7

File hashes

Hashes for pysnapcode-0.1.1.tar.gz
Algorithm Hash digest
SHA256 6772a965a7c1ad884cbd6c3d5d1635a5f036327aab6f8a490d2e4f056b2678f0
MD5 2b6ea02f75ea0f0f7e949241e8ae4b1a
BLAKE2b-256 76ccb44a543a09159046b30bdfa0893e21acfb2350738bf50e344298a84352b4

See more details on using hashes here.

File details

Details for the file pysnapcode-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: pysnapcode-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 6.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.7

File hashes

Hashes for pysnapcode-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 abc2bec79610a47891de5696cfa5f96f9220a74b8a8e9f6c1e8dcd6857aa5e17
MD5 4a54379beb476344f764db00cb104e24
BLAKE2b-256 cde631dc03bbb1ca451b12fd04dd3992ef7cca6a23c87fd8f65d952c6d2a56f8

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page