Skip to content

Instantly share code, notes, and snippets.

@waylan
waylan / bootstrap_alert.py
Last active March 2, 2023 14:26
A Markdown extension which adds support for bootstrap alerts and (simple) carousels using pymdownx blocks v9.10b5.
View bootstrap_alert.py
from pymdownx.blocks import BlocksExtension
from pymdownx.blocks.block import Block, type_html_attribute_dict, type_html_identifier, type_string_in, \
type_boolean, type_string, , type_multi
import xml.etree.ElementTree as etree
import uuid
class BsAlertBlock(Block):
NAME = 'alert'
ARGUMENT = None
View rename_ical.py
import icalendar
def rename_events(infile, outfile, names=None):
'''
Given a path to a ics file, rename each event and write to outfile.
names should be a list of strings where each item is the new name
to assign to each event in the ics file. If an insuffient number
of names are provided, then an error will occur.
@waylan
waylan / print_label.py
Created October 19, 2021 12:56
Print labels directly to Brother QL label printer using brother_ql lib.
View print_label.py
from PIL import Image, ImageDraw, ImageFont
from brother_ql.devicedependent import label_type_specs
from brother_ql import BrotherQLRaster, create_label
from brother_ql.backends.helpers import send
PRINTER = {
'model': 'QL-800',
'address': 'usb://0x04f9:0x209b'
}
@waylan
waylan / text2img.py
Last active September 17, 2021 13:22
Create an image of text, which is centered both ways. Useful for creating a label.
View text2img.py
from PIL import Image, ImageDraw, ImageFont
def create_image(text, size, fp, format=None):
img = Image.new('L', size, 'white')
draw = ImageDraw.Draw(img)
# Find largest font size which fits within image size
fontsize = 20
margin = 10
@waylan
waylan / peewee_monthfield.py
Created September 10, 2021 18:53
A MonthField for the peewee ORM. Inspired by https://github.com/clearspark/django-monthfield.
View peewee_monthfield.py
from peewee import _BaseFormattedField, format_date_time, _date_part
import datetime
class MonthField(_BaseFormattedField):
field_type = 'DATE'
formats = [
'%Y-%m',
'%Y-%m-%d',
'%Y-%m-%d %H:%M:%S',
View md2wpdf.py
import markdown
import re
import yaml
from yaml import SafeLoader
from wp2pdf import html2pdf
BLOCK_RE = re.compile(r'^-{3}[ \t]*\n(.*?\n)(?:\.{3}|-{3})[ \t]*\n', re.UNICODE|re.DOTALL)
@waylan
waylan / wp2pdf.py
Last active June 30, 2021 17:54
An HTML to PDF script using Weasyprint.
View wp2pdf.py
from weasyprint import HTML, CSS
HTML_TEMPLATE = """
<html>
<head>
</head>
<body>
{body}
</body>
</html>
View pdf_columns.py
from weasyprint import HTML
CONTENT = """
<html>
<head>
<style>
h1 {
padding: .5em 0;
background-color: lightskyblue;
}
@waylan
waylan / textfield.py
Last active September 16, 2022 13:54
A PDF fillable textfield which flows with text on a page using Reportlab.
View textfield.py
from reportlab.platypus import SimpleDocTemplate, Flowable, Paragraph
from reportlab.lib.styles import getSampleStyleSheet
style = getSampleStyleSheet()['BodyText']
class TextField(Flowable):
def __init__(self, **options):
Flowable.__init__(self)
@waylan
waylan / md2pdf.py
Created June 9, 2021 20:18
A simple Markdown to PDF converter.
View md2pdf.py
import markdown
from xhtml2pdf import pisa
from datetime import date
TEMPLATE = """
<html>
<head>
<style>
@page {{
size: letter portrait;