Skip to content

Instantly share code, notes, and snippets.

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 Oct 19, 2021
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 Sep 17, 2021
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
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 Jun 30, 2021
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 Sep 16, 2022
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 Jun 9, 2021
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;
@waylan
waylan / service_year.py
Created Feb 22, 2021
A Python object which represents a year that runs from Sept to Aug.
View service_year.py
import datetime
class ServiceYear():
'''
A year which runs from Sept to Aug.
Accepts a year in any format which can be converted to an integer.
>>> year = ServiceYear(2021)