18
20

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

XLSX を PDF に変換 (openpyxl,reportlab)

Posted at

openpyxl と reportlab を使って、XLSX を PDF に変換します。
pandas と FPDF を使う例はこちら
XLSX を PDF に変換 (pandas,FPDF)

xlsx_pdf_simple.py
#! /usr/bin/python
#
#	xlsx_pdf.py
#
#					   Feb/03/2019
# ------------------------------------------------------------------
import sys
from openpyxl import Workbook
from openpyxl import load_workbook
from reportlab.pdfbase.cidfonts import UnicodeCIDFont
from reportlab.pdfbase import pdfmetrics
from reportlab.platypus import SimpleDocTemplate, Table, TableStyle
from reportlab.lib.pagesizes import A4
from reportlab.lib import colors
# ------------------------------------------------------------------
# [4]:
def pdf_gen_proc(file_pdf,ws):
	doc = SimpleDocTemplate(file_pdf, pagesize=A4)
	fontname_g = "HeiseiKakuGo-W5"
	pdfmetrics.registerFont(UnicodeCIDFont(fontname_g))
	elements = []
#
	data = []
	for row in ws.rows:
		unit_aa = []
		print(row[0].value)
		for col in row:
			unit_aa.append(col.value)
		data.append(unit_aa)
	tt=Table(data)
	tt.setStyle(TableStyle([('BACKGROUND',(1,1),(-2,-2),colors.cyan),
		('TEXTCOLOR',(0,0),(1,-1),colors.red),
		('FONT', (0, 0), (-1, -1), "HeiseiKakuGo-W5", 20),
		('GRID', (0, 0), (ws.max_column, ws.max_row), 0.25, colors.black),]))
	elements.append(tt)
#
	doc.build(elements)
# ------------------------------------------------------------------
sys.stderr.write("*** 開始 ***\n")
file_xlsx = sys.argv[1]
file_pdf = sys.argv[2]
#
wb = load_workbook(filename = file_xlsx)
ws = wb.active
print("ws.max_row = %d" % ws.max_row)
print("ws.max_column = %d" % ws.max_column)
#
pdf_gen_proc(file_pdf,ws)
#
#
sys.stderr.write("*** 終了 ***\n")
# ------------------------------------------------------------------

実行方法

./xlsx_pdf_simple.py cities.xlsx out01.pdf
18
20
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
18
20

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?