Skip to content

Instantly share code, notes, and snippets.

@NaoY-2501
Last active September 24, 2018 07:37
Show Gist options
  • Save NaoY-2501/ad3746743d81984e3ed11104729d04c8 to your computer and use it in GitHub Desktop.
Save NaoY-2501/ad3746743d81984e3ed11104729d04c8 to your computer and use it in GitHub Desktop.
PythonでQRコードを生成してSVGタグに変換する
import xml.dom.minidom
import qrcode
import qrcode.image.svg as svg
def make_qrcode_svg(text):
"""
QRコードのSVGを作る関数
:param text: QRコードに埋め込みたい文字列
:return: QRコードのSVG
"""
factory = svg.SvgPathImage
img = qrcode.make(
text,
image_factory=factory,
version=1,
box_size=6,
error_correction=qrcode.constants.ERROR_CORRECT_L)
return img
def set_dom_attr(dom, elem, values):
"""
属性ノードを生成して要素を設定する関数
:param dom: xml.dom.minidom.Document Object
:param elem: 要素
:param values: 属性
:return:
"""
for attr, value in values.items():
# 属性ノードを生成
subnode_attrs = dom.createAttribute(attr)
# 属性ノードの値を設定
subnode_attrs.value = value
# 属性ノードを要素ノードにセットする
elem.setAttributeNode(subnode_attrs)
def create_dom(img):
"""
QRコードのsvgをXMLとして出力する関数
:param img: QRコードのsvg
:return dom: xml.dom.minidom.Document Object
"""
width = img.width
items = img.get_image()
path = img.make_path()
# DOM
dom = xml.dom.minidom.Document()
# svg要素を作成
elem = dom.createElement('svg')
# DOMに子要素elemを追加
dom.appendChild(elem)
# svgからxmlns属性を取得
xmlns = items.get('xmlns')
# svg要素の属性を指定
values = {
'width': f'{width}mm',
'height': f'{width}mm',
'viewBox': f'0 0 {width} {width}',
'xmlns': xmlns
}
# DOMにsvg要素を設定
set_dom_attr(dom, elem, values)
# path要素を作成
path_node = dom.createElement('path')
# elem要素に子要素(path_node)を追加
elem.appendChild(path_node)
# pathからd属性を取得
d = path.get('d')
# pathからid属性を取得
id_ = path.get('id')
# pathからstyle属性を取得
style = path.get('style')
# path要素の属性を指定
values = {
'd': d,
'id': id_,
'style': style,
}
# DOMにpath要素を設定
set_dom_attr(dom, path_node, values)
return dom
def main():
# QRコードに埋め込むテキストの読み込み
with open('zen.txt', encoding='utf-8') as f:
text = f.read()
# QRコードのSVG作成
qr = make_qrcode_svg(text)
# DOMオブジェクトを作成
dom = create_dom(qr)
# DOMをXMLに整形
xml = dom.toprettyxml()
HTML = f'''
<!DOCTYPE html>
<head>
<title>sample QR</title>
</head>
<body>
<h1>The Zen of Python</h1>
{xml}
</body>
</html>
'''
# HTMLに埋め込み
with open('sample.html', 'w', encoding='utf-8') as f:
f.write(HTML)
if __name__ == '__main__':
main()
The Zen of Python, by Tim Peters
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment