๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

Python/Basic

[Python] PDF ๊ด€๋ จ ํŒจํ‚ค์ง€ ์ •๋ฆฌ

๋ฐ˜์‘ํ˜•

PDFRW

 

https://github.com/pmaupin/pdfrw

 

GitHub - pmaupin/pdfrw: pdfrw is a pure Python library that reads and writes PDFs

pdfrw is a pure Python library that reads and writes PDFs - GitHub - pmaupin/pdfrw: pdfrw is a pure Python library that reads and writes PDFs

github.com

 

https://kimhwon.github.io/pdfrw-automation/

 

pdfrw๋ฅผ ์‚ฌ์šฉํ•œ PDF ์—…๋ฌด ์ž๋™ํ™”

PDF ํผ์„ ์ฑ„์šฐ๋Š” ์—…๋ฌด๋ฅผ ๋ฐ›์•˜์Šต๋‹ˆ๋‹ค. ๋Œ€๋žต 600 rows ์ฏค ๋˜๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ๋ถ„๋ฅ˜ํ•ด์„œ PDF์— ์ฑ„์›Œ ๋„ฃ๋Š” ์—…๋ฌด์˜€์Šต๋‹ˆ๋‹ค. ๋‹น์—ฐํžˆ ์†์œผ๋กœ ์ผ์ผํžˆ ์ฑ„์šฐ๊ธฐ๋Š” ๋ถˆํŽธํ•˜๊ธฐ ๋•Œ๋ฌธ์— Python์„ ์‚ฌ์šฉํ•ด์„œ ์ž๋™ํ™”๋ฅผ ํ•ด๋ณด์•˜์Šต

kimhwon.github.io

 

 

 

FPDF2

 

import csv
from fpdf import FPDF


class PDF(FPDF):
    def basic_table(self, headings, rows):
        for heading in headings:
            self.cell(40, 7, heading, 1)
        self.ln()
        for row in rows:
            for col in row:
                self.cell(40, 6, col, 1)
            self.ln()

    def improved_table(self, headings, rows, col_widths=(42, 39, 35, 40)):
        for col_width, heading in zip(col_widths, headings):
            self.cell(col_width, 7, heading, border=1, align="C")
        self.ln()
        for row in rows:
            self.cell(col_widths[0], 6, row[0], border="LR")
            self.cell(col_widths[1], 6, row[1], border="LR")
            self.cell(col_widths[2], 6, row[2], border="LR", align="R")
            self.cell(col_widths[3], 6, row[3], border="LR", align="R")
            self.ln()
        # Closure line:
        self.cell(sum(col_widths), 0, "", border="T")

    def colored_table(self, headings, rows, col_widths=(42, 39, 35, 42)):
        # Colors, line width and bold font:
        self.set_fill_color(255, 100, 0)
        self.set_text_color(255)
        self.set_draw_color(255, 0, 0)
        self.set_line_width(0.3)
        self.set_font(style="B")
        for col_width, heading in zip(col_widths, headings):
            self.cell(col_width, 7, heading, border=1, align="C", fill=True)
        self.ln()
        # Color and font restoration:
        self.set_fill_color(224, 235, 255)
        self.set_text_color(0)
        self.set_font()
        fill = False
        for row in rows:
            self.cell(col_widths[0], 6, row[0], border="LR", align="L", fill=fill)
            self.cell(col_widths[1], 6, row[1], border="LR", align="L", fill=fill)
            self.cell(col_widths[2], 6, row[2], border="LR", align="R", fill=fill)
            self.cell(col_widths[3], 6, row[3], border="LR", align="R", fill=fill)
            self.ln()
            fill = not fill
        self.cell(sum(col_widths), 0, "", "T")


def load_data_from_csv(csv_filepath):
    headings, rows = [], []
    with open(csv_filepath, encoding="utf8") as csv_file:
        for row in csv.reader(csv_file, delimiter=","):
            if not headings:  # extracting column names from first row:
                headings = row
            else:
                rows.append(row)
    return headings, rows


col_names, data = load_data_from_csv("countries.txt")
pdf = PDF()
pdf.set_font("helvetica", size=14)
pdf.add_page()
pdf.basic_table(col_names, data)
pdf.add_page()
pdf.improved_table(col_names, data)
pdf.add_page()
pdf.colored_table(col_names, data)
pdf.output("tuto5.pdf")

 

Adding content onto an existing PDF page

import sys
from fpdf import FPDF
from pdfrw import PageMerge, PdfReader, PdfWriter

IN_FILEPATH = sys.argv[1]
OUT_FILEPATH = sys.argv[2]
ON_PAGE_INDEX = 1
UNDERNEATH = False  # if True, new content will be placed underneath page (painted first)

def new_content():
    fpdf = FPDF()
    fpdf.add_page()
    fpdf.set_font("helvetica", size=36)
    fpdf.text(50, 50, "Hello!")
    reader = PdfReader(fdata=bytes(fpdf.output()))
    return reader.pages[0]

reader = PdfReader(IN_FILEPATH)
writer = PdfWriter()
writer.pagearray = reader.Root.Pages.Kids
PageMerge(writer.pagearray[ON_PAGE_INDEX]).add(new_content(), prepend=UNDERNEATH).render()
writer.write(OUT_FILEPATH)

 

 

 

 

https://pyfpdf.github.io/fpdf2/ExistingPDFs.html

 

fpdf2 - Existing PDFs

fpdf2 - minimalist PDF creation library for Python

pyfpdf.github.io

 

 

PyPDF2

 

from PyPDF2 import PdfReader, PdfWriter
from PyPDF2.generic import AnnotationBuilder

# Fill the writer with the pages you want
pdf_path = os.path.join(RESOURCE_ROOT, "crazyones.pdf")
reader = PdfReader(pdf_path)
page = reader.pages[0]
writer = PdfWriter()
writer.add_page(page)

# Create the annotation and add it
annotation = AnnotationBuilder.free_text(
    "Hello World\nThis is the second line!",
    rect=(50, 550, 200, 650),
    font="Arial",
    bold=True,
    italic=True,
    font_size="20pt",
    font_color="00ff00",
    border_color="0000ff",
    bg_color="cdcdcd",
)
writer.add_annotation(page_number=0, annotation=annotation)

# Write the annotated file to disk
with open("annotated-pdf.pdf", "wb") as fp:
    writer.write(fp)

 

https://pypdf2.readthedocs.io/

 

Welcome to PyPDF2 — PyPDF2 documentation

© Copyright 2006 - 2008, Mathieu Fenniak. Revision 2ddc48a8.

pypdf2.readthedocs.io

 

๋ฐ˜์‘ํ˜•