Repository navigation
Expand file tree
/
Copy pathdecode_message.py
More file actions
60 lines (46 loc) · 1.64 KB
/
Copy pathdecode_message.py
File metadata and controls
60 lines (46 loc) · 1.64 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
import requests
from bs4 import BeautifulSoup
def render_grid_from_doc(url: str):
# Step 1: Download the Google Doc HTML
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
# Step 2: Find the first <table> element
table = soup.find("table")
if not table:
print("No table found.")
return
rows = table.find_all("tr")
pixels = []
# Step 3: Extract valid rows
for row in rows:
cells = row.find_all("td")
if len(cells) != 3:
continue # Skip malformed rows
x_text = cells[0].text.strip()
char = cells[1].text.strip()
y_text = cells[2].text.strip()
if not (x_text.isdigit() and y_text.isdigit()):
continue # Skip header or bad rows
x = int(x_text)
y = int(y_text)
pixels.append((x, y, char))
# Debug print to verify parsing
print("Pixels found:", len(pixels))
print("First 5 pixels:", pixels[:5])
if not pixels:
print("No valid pixel data found. Check the table structure.")
return
# Step 4: Determine grid size
max_x = max(x for x, y, _ in pixels)
max_y = max(y for x, y, _ in pixels)
# Step 5: Initialize grid
grid = [[' ' for _ in range(max_x + 1)] for _ in range(max_y + 1)]
# Step 6: Populate grid
for x, y, char in pixels:
grid[y][x] = char
# Step 7: Print the result
for row in grid:
print(''.join(row))
# Run it with your provided URL
url = "https://docs.google.com/document/d/e/2PACX-1vTER-wL5E8YC9pxDx43gk8eIds59GtUUk4nJo_ZWagbnrH0NFvMXIw6VWFLpf5tWTZIT9P9oLIoFJ6A/pub"
render_grid_from_doc(url)