-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcsv_to_pointid_pdfs.py
More file actions
179 lines (147 loc) · 5.31 KB
/
csv_to_pointid_pdfs.py
File metadata and controls
179 lines (147 loc) · 5.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import os
import glob
import pandas as pd
from reportlab.lib.pagesizes import letter
from reportlab.platypus import (
SimpleDocTemplate,
Paragraph,
Spacer,
Table,
TableStyle,
Image,
)
from reportlab.lib.styles import getSampleStyleSheet
from reportlab.lib import colors
from reportlab.lib.units import inch
# === CONFIG ===
CSV_PATH = r"datarequests/monica_gw_request_20251118.csv" # adjust if needed
PHOTOS_DIR = r"\\agustin\amp\data\database\photos\Digital photos_wells"
OUTPUT_DIR = r"output_pdfs" # folder where individual PDFs are written
def make_output_dir(path: str):
if not os.path.exists(path):
os.makedirs(path)
def aggregate_group_to_record(group: pd.DataFrame) -> dict:
"""
For a group of rows with the same PointID, collapse each column into a
single string (join unique non-null values with '; ').
"""
record = {}
for col in group.columns:
if col == "PointID":
continue
# Unique non-empty values
vals = [
str(v)
for v in group[col].dropna().unique()
if str(v).strip() != ""
]
if not vals:
continue
record[col] = "; ".join(vals)
return record
def find_photos_for_point(point_id: str) -> list:
"""
Return a list of photo file paths for the given PointID.
PointID in CSV has hyphens (e.g. 'WL-0224'), but photos are
stored without hyphens (e.g. 'WL0224*.jpg').
"""
clean_id = point_id.replace("-", "")
pattern = os.path.join(PHOTOS_DIR, f"{clean_id}*.jpg")
return sorted(glob.glob(pattern))
from reportlab.lib.styles import ParagraphStyle
from reportlab.platypus import Paragraph, Table, TableStyle, Image, Spacer
from reportlab.lib import colors
from reportlab.lib.units import inch
def build_pdf_for_point(point_id: str, group: pd.DataFrame, output_dir: str):
"""
Create a single PDF for one PointID, including:
- Header with PointID
- Table of field/value pairs (wrapped text)
- All matching photos
"""
# Sanitize filename (in case of weird chars)
safe_point_id = point_id.replace("/", "_").replace("\\", "_")
pdf_path = os.path.join(output_dir, f"{safe_point_id}.pdf")
doc = SimpleDocTemplate(pdf_path, pagesize=letter)
styles = getSampleStyleSheet()
elements = []
# Title
elements.append(Paragraph(f"Point ID: {point_id}", styles["Title"]))
elements.append(Spacer(1, 12))
# Aggregate field values
record = aggregate_group_to_record(group)
# Paragraph style for table cells (wrap long text)
cell_style = ParagraphStyle(
"Cell",
parent=styles["Normal"],
fontSize=8,
leading=9,
wordWrap="CJK", # better at wrapping long strings / URLs
)
# Build table: Field | Value
data = [["Field", "Value"]] # header row
for field_name, value in record.items():
# Wrap the value in a Paragraph so it will line-wrap
value_paragraph = Paragraph(str(value), cell_style)
field_paragraph = Paragraph(str(field_name), cell_style)
data.append([field_paragraph, value_paragraph])
table = Table(data, colWidths=[2.0 * inch, 4.5 * inch])
table.setStyle(
TableStyle(
[
("BACKGROUND", (0, 0), (-1, 0), colors.lightgrey),
("TEXTCOLOR", (0, 0), (-1, 0), colors.black),
("ALIGN", (0, 0), (-1, -1), "LEFT"),
("FONTNAME", (0, 0), (-1, 0), "Helvetica-Bold"),
("FONTSIZE", (0, 0), (-1, 0), 9),
("BOTTOMPADDING", (0, 0), (-1, 0), 6),
("GRID", (0, 0), (-1, -1), 0.25, colors.grey),
]
)
)
elements.append(table)
elements.append(Spacer(1, 18))
# Photos
photo_files = find_photos_for_point(point_id)
if photo_files:
elements.append(Paragraph("Photos", styles["Heading2"]))
elements.append(Spacer(1, 6))
for photo_path in photo_files:
try:
img = Image(photo_path)
# Restrict size to fit page width
img._restrictSize(6.5 * inch, 8.0 * inch)
elements.append(img)
elements.append(
Paragraph(os.path.basename(photo_path), styles["Normal"])
)
elements.append(Spacer(1, 12))
except Exception as exc:
elements.append(
Paragraph(
f"Could not load image: {os.path.basename(photo_path)} "
f"({exc})",
styles["Normal"],
)
)
elements.append(Spacer(1, 6))
else:
elements.append(
Paragraph("No photos found for this PointID.", styles["Italic"])
)
doc.build(elements)
print(f"Created PDF for {point_id}: {pdf_path}")
def main():
make_output_dir(OUTPUT_DIR)
# Read CSV
df = pd.read_csv(CSV_PATH)
if "PointID" not in df.columns:
raise ValueError("CSV must contain a 'PointID' column.")
# Group by PointID and build one PDF per group
grouped = df.groupby("PointID", dropna=True)
for point_id, group in grouped:
if pd.isna(point_id):
continue
build_pdf_for_point(str(point_id), group, OUTPUT_DIR)
if __name__ == "__main__":
main()