0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

長いテキストを分割して複数のQRコードにするPythonコード

Posted at

使用イメージ

以下のようなファイルを対象に
image.png

以下のQRコードを生成。(テキストが長い場合は複数のQRコードに分割)
アプリでQRコードを読み込んで、テキストをネットを介さず転記
image.png

動機

  • インターネットを介さずテキストデータを転送したい
  • 写真見ながらの転記はさすがに面倒

使い方

  • 以下のコマンドでqrcodeライブラリをインストールする
pip install "qrcode[pil]"
  • "QrGenerate.py"ファイルを作成してエディタでソースコードを張り付ける
  • file_name 変数をQRコード化したいファイルとなるように変更してから実行
  • 生成したQRコードをレンズアプリなどで読み込んで、テキストを転記

ソースコード(Pythonファイル)

import qrcode
import math

# 分割サイズ(文字数): 1000文字程度が安全
CHUNK_SIZE = 1000

file_name = "QrGenerate.py"

# ファイルを開いて内容を読み込む
with open(file_name, "r", encoding="utf-8") as f:
    data = f.read()

# 分割数を計算
total_chunks = math.ceil(len(data) / CHUNK_SIZE)

if total_chunks == 1:
    # 分割不要な場合
    img = qrcode.make(data)
    img.save("file_qr.png")
    img.show()
    print("1つのQRコードに保存しました: file_qr.png")
else:
    # 分割してQRコードを作成
    for i in range(total_chunks):
        chunk = data[i*CHUNK_SIZE:(i+1)*CHUNK_SIZE]
        # 各分割にインデックス情報を付加(例: 1/5:データ)
        qr_data = f"{i+1}/{total_chunks}:{chunk}"
        img = qrcode.make(qr_data)
        fname = f"file_qr_{i+1:03d}.png"
        img.save(fname)
        print(f"{fname} を作成しました")

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?