Pythonは、近年のAI(人工知能)ブームなど、データサイエンス分野で注目を集めるプログラミング言語です。シンプルで習得が容易なことを目指し、言語機能は最小限、不足分は拡張ライブラリで補う方針で開発されました。
人工知能に加え、拡張ライブラリの中でも統計や解析分野が充実し、ビッグデータの中から価値のある情報を見つけ出すデータ解析や分析ツール、車の自動運転や家屋内のエネルギー消費の最適化などIoT(アイオーティー)分野での利用が盛んに行われています。
Webアプリ開発にも力を入れており、YouTubeやインスタグラム、Dropboxなどの開発言語としても知られています。
Pythonは、「Python2」と「Python3」の2つのバージョンがありますが、「Python3」のほうが新バージョンで、新しい開発は「Python3」で行われていることが多いようです。
数式、統計処理の拡張ライブラリが充実しているPythonは、機械学習、ディープラーニング、科学技術計算などデータサイエンス業界で必要とされています。
自動車や住宅、建築、服飾などの設計や製図を支援するシステムであるCAD、3Dでデータを作成する3Dモデリング、WEBアプリケーション開発にも使われ、幅広い分野での活躍が期待できるでしょう。
Pythonのサンプルコード
PDFページへ画像を挿入する
コードがシンタックスハイライトされてない場合は、ページを再読み込みしてください。
import fitz # <-- PyMuPDF
doc = fitz.open("some.pdf") # open the PDF
rect = fitz.Rect(0, 0, 100, 100) # where to put image: use upper left corner
for page in doc:
page.insertImage(rect, filename = "some.image")
doc.saveIncr() # do an incremental save
PDFページにテキストボックスを挿入する
import fitz # import PyMuPDF
doc = fitz.open("some.pdf") # or new: fitz.open(), followed by insertPage()
page = doc[n] # choose some page
rect = fitz.Rect(50, 100, 300, 400) # rectangle (left, top, right, bottom) in pixels
text = """This text will only appear in the rectangle. Depending on width, new lines are generated as required.\n<- This forced line break will also appear.\tNow a very long word: abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.\nIt will be broken into pieces."""
rc = page.insertTextbox(rect, text, fontsize = 12, # choose fontsize (float)
fontname = "Times-Roman", # a PDF standard font
fontfile = None, # could be a file on your system
align = 0) # 0 = left, 1 = center, 2 = right
print("unused rectangle height: %g" % rc) # just demo (should display "44.2")
doc.saveIncr() # update file. Save to new instead by doc.save("new.pdf",...)
数行でPDFにカレンダーを作成する
mport fitz
import calendar
import sys
assert len(sys.argv) == 2, "need start year as the one and only parameter"
startyear = sys.argv[1]
assert startyear.isdigit(), "year must be positive numeric"
startyear = int(startyear)
assert startyear > 0, "year must be positive numeric"
doc = fitz.open()
cal = calendar.LocaleTextCalendar(locale = "de") # choose your locale
w, h = fitz.PaperSize("a4-l") # get sizes for A4 landscape paper
txt = cal.formatyear(startyear, m = 4)
doc.insertPage(-1, txt, fontsize = 12, fontname = "Courier", width = w, height = h)
txt = cal.formatyear(startyear + 1, m = 4)
doc.insertPage(-1, txt, fontsize = 12, fontname = "Courier", width = w, height = h)
txt = cal.formatyear(startyear + 2, m = 4)
doc.insertPage(-1, txt, fontsize = 12, fontname = "Courier", width = w, height = h)
doc.save("Kalender.pdf", garbage = 4, deflate = True)
Pythonのフレームワーク
Pythonは、Webアプリケーション開発のためのフレームワークがあります。
大規模向けの「Django」
Pythonの代表的なフレームワークとして知られているのは、大規模向けの「Django」。
「Django」は、迅速な開発で実用的なデザインを促進するフレームワークです。
データベースやセキュリティ、ユーザーインターフェースの機能をひとつのアプリケーションでまかなうフルスタックで、高い柔軟性とパフォーマンスが特徴。
小規模から中規模向けの「Bottle」
小規模から中規模向けのフレームワークとしては、「Bottle」、「Flask」が良く知られています。
「Bottle」は、マイクロWEBフレームワーク。学習量が少なくすみ、「今すぐ何かを作りたい」という開発者にとっては格好のフレームワークといえます。
小規模から中規模向けの「Flask」
「Flask」は、マイクロWEBフレームワークの中でも軽量のフレームワークです。コミュニティが提供する多くの拡張機能があり、新しい機能をシンプルに追加できるのが特長です。