Skip to content

Commit e30b568

Browse files
committed
Black formatting pass, fix some linter errors
1 parent 9c0d365 commit e30b568

File tree

6 files changed

+95
-43
lines changed

6 files changed

+95
-43
lines changed

creating-and-modifying-pdfs/source_code/01-extracting-text-from-a-pdf.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
# ---------------
32
# Open a PDF File
43
# ---------------
@@ -7,11 +6,12 @@
76

87
# You might need to change this to match the path on your computer
98
from pathlib import Path
9+
1010
pdf_path = (
11-
Path.home() /
12-
"creating-and-modifying-pdfs" /
13-
"practice_files" /
14-
"Pride_and_Prejudice.pdf"
11+
Path.home()
12+
/ "creating-and-modifying-pdfs"
13+
/ "practice_files"
14+
/ "Pride_and_Prejudice.pdf"
1515
)
1616

1717
pdf = PdfFileReader(str(pdf_path))
@@ -46,10 +46,10 @@
4646

4747
# Change the path below to the correct path for your computer.
4848
pdf_path = (
49-
Path.home() /
50-
"creating-and-modifying-pdfs" /
51-
"practice-files" /
52-
"Pride_and_Prejudice.pdf"
49+
Path.home()
50+
/ "creating-and-modifying-pdfs"
51+
/ "practice-files"
52+
/ "Pride_and_Prejudice.pdf"
5353
)
5454

5555
pdf_reader = PdfFileReader(str(pdf_path))

creating-and-modifying-pdfs/source_code/02-extracting-pages-from-a-pdf.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@
33
# -----------------------------
44

55
from PyPDF2 import PdfFileWriter
6+
67
pdf_writer = PdfFileWriter()
78

89
page = pdf_writer.addBlankPage(width=72, height=72)
910

1011
print(type(page))
1112

1213
from pathlib import Path
14+
1315
with Path("blank.pdf").open(mode="wb") as output_file:
1416
pdf_writer.write(output_file)
1517

@@ -23,10 +25,10 @@
2325

2426
# Change the path to work on your computer if necessary
2527
pdf_path = (
26-
Path.home() /
27-
"creating-and-modifying-pdfs" /
28-
"practice_files" /
29-
"Pride_and_Prejudice.pdf"
28+
Path.home()
29+
/ "creating-and-modifying-pdfs"
30+
/ "practice_files"
31+
/ "Pride_and_Prejudice.pdf"
3032
)
3133
input_pdf = PdfFileReader(str(pdf_path))
3234

@@ -47,10 +49,10 @@
4749
from pathlib import Path
4850

4951
pdf_path = (
50-
Path.home() /
51-
"creating-and-modifying-pdfs" /
52-
"practice_files" /
53-
"Pride_and_Prejudice.pdf"
52+
Path.home()
53+
/ "creating-and-modifying-pdfs"
54+
/ "practice_files"
55+
/ "Pride_and_Prejudice.pdf"
5456
)
5557
input_pdf = PdfFileReader(str(pdf_path))
5658

@@ -64,7 +66,7 @@
6466
pdf_writer = PdfFileWriter()
6567

6668
for page in input_pdf.pages[1:4]:
67-
pdf_writer.addPage(page)
69+
pdf_writer.addPage(page)
6870

6971
with Path("chapter1_slice.pdf").open(mode="wb") as output_file:
70-
pdf_writer.write(output_file)
72+
pdf_writer.write(output_file)

creating-and-modifying-pdfs/source_code/03-concatenating-and-merging-pdfs.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,20 @@
33
# -----------------------------
44

55
from PyPDF2 import PdfFileMerger
6+
67
pdf_merger = PdfFileMerger()
78

89
# ---------------------------------
910
# Concatenating PDFs With .append()
1011
# ---------------------------------
1112

1213
from pathlib import Path
14+
1315
reports_dir = (
14-
Path.home() /
15-
"creating-and-modifying-pdfs" /
16-
"practice_files" /
17-
"expense_reports"
16+
Path.home()
17+
/ "creating-and-modifying-pdfs"
18+
/ "practice_files"
19+
/ "expense_reports"
1820
)
1921

2022
for path in reports_dir.glob("*.pdf"):
@@ -41,10 +43,10 @@
4143
from PyPDF2 import PdfFileMerger
4244

4345
report_dir = (
44-
Path.home() /
45-
"creating-and-modifying-pdfs" /
46-
"practice_files" /
47-
"quarterly_report"
46+
Path.home()
47+
/ "creating-and-modifying-pdfs"
48+
/ "practice_files"
49+
/ "quarterly_report"
4850
)
4951

5052
report_path = report_dir / "report.pdf"
@@ -56,4 +58,4 @@
5658
pdf_merger.merge(1, str(toc_path))
5759

5860
with Path("full_report.pdf").open(mode="wb") as output_file:
59-
pdf_merger.write(output_file)
61+
pdf_merger.write(output_file)

creating-and-modifying-pdfs/source_code/04-rotating-and-cropping-PDF-pages.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,7 @@
66
from PyPDF2 import PdfFileReader, PdfFileWriter
77

88
pdf_path = (
9-
Path.home() /
10-
"creating-and-modifying-pdfs" /
11-
"practice_files" /
12-
"ugly.pdf"
9+
Path.home() / "creating-and-modifying-pdfs" / "practice_files" / "ugly.pdf"
1310
)
1411

1512
pdf_reader = PdfFileReader(str(pdf_path))
@@ -60,10 +57,10 @@
6057
from PyPDF2 import PdfFileReader, PdfFileWriter
6158

6259
pdf_path = (
63-
Path.home() /
64-
"creating-and-modifying-pdfs" /
65-
"practice_files" /
66-
"half_and_half.pdf"
60+
Path.home()
61+
/ "creating-and-modifying-pdfs"
62+
/ "practice_files"
63+
/ "half_and_half.pdf"
6764
)
6865

6966
pdf_reader = PdfFileReader(str(pdf_path))
@@ -92,6 +89,7 @@
9289
first_page = pdf_reader.getPage(0)
9390

9491
import copy
92+
9593
left_side = copy.deepcopy(first_page)
9694
current_coords = left_side.mediaBox.upperRight
9795
new_coords = (current_coords[0] / 2, current_coords[1])
@@ -103,4 +101,4 @@
103101
pdf_writer.addPage(left_side)
104102
pdf_writer.addPage(right_side)
105103
with Path("cropped_pages.pdf").open(mode="wb") as output_file:
106-
pdf_writer.write(output_file)
104+
pdf_writer.write(output_file)

creating-and-modifying-pdfs/source_code/05-encrypting-and-decrypting-pdfs.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
from PyPDF2 import PdfFileReader, PdfFileWriter
77

88
pdf_path = (
9-
Path.home() /
10-
"creating-and-modifying-pdfs" /
11-
"practice_files" /
12-
"newsletter.pdf"
9+
Path.home()
10+
/ "creating-and-modifying-pdfs"
11+
/ "practice_files"
12+
/ "newsletter.pdf"
1313
)
1414

1515
pdf_reader = PdfFileReader(str(pdf_path))
@@ -43,4 +43,4 @@
4343

4444
print(pdf_reader.decrypt(password="SuperSecret"))
4545

46-
print(pdf_reader.getPage(0))
46+
print(pdf_reader.getPage(0))

creating-and-modifying-pdfs/source_code/06-creating-a-pdf-file-from-scratch.py

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,54 @@
22
# Using the Canvas Class
33
# ----------------------
44

5-
from reportlab.pdfgen.canvas import Canvas
5+
from reportlab.pdfgen.canvas import Canvas
6+
7+
canvas = Canvas("hello.pdf")
8+
canvas.drawString(72, 72, "Hello, World")
9+
canvas.save()
10+
11+
12+
# ---------------------
13+
# Setting the Page Size
14+
# ---------------------
15+
16+
from reportlab.lib.units import inch, cm
17+
18+
print(cm)
19+
print(inch)
20+
21+
canvas = Canvas("hello.pdf", pagesize=(8.5 * inch, 11 * inch))
22+
23+
from reportlab.lib.pagesizes import LETTER
24+
25+
canvas = Canvas("hello.pdf", pagesize=LETTER)
26+
print(LETTER)
27+
28+
29+
# -----------------------
30+
# Setting Font Properties
31+
# -----------------------
32+
33+
canvas = Canvas("font-example.pdf", pagesize=LETTER)
34+
canvas.setFont("Times-Roman", 18)
35+
canvas.drawString(1 * inch, 10 * inch, "Times New Roman (18 pt)")
36+
canvas.save()
37+
38+
# The code below creates a PDF with blue text
39+
from reportlab.lib.colors import blue
40+
from reportlab.lib.pagesizes import LETTER
41+
from reportlab.lib.units import inch
42+
from reportlab.pdfgen.canvas import Canvas
43+
44+
canvas = Canvas("font-colors.pdf", pagesize=LETTER)
45+
46+
# Set font to Times New Roman with 12-point size
47+
canvas.setFont("Times-Roman", 12)
48+
49+
# Draw blue text one inch from the left and ten
50+
# inches from the bottom
51+
canvas.setFillColor(blue)
52+
canvas.drawString(1 * inch, 10 * inch, "Blue text")
53+
54+
# Save the PDF file
55+
canvas.save()

0 commit comments

Comments
 (0)