1 / Data:
JavaScript
x
6
1
Column A Column B Column C
2
AAA 1230 CCC
3
ABA 4560 CDC
4
AAb 7890 CCD
5
6
2 / Set-up (pandas):
JavaScript
1
6
1
import pandas as pd
2
from pandas import ExcelWriter
3
from pandas import ExcelFile
4
5
df = pd.read_excel("data.xlsx", sheet_name="Sheet1")
6
3 / Formatting (e.g. for first row):
JavaScript
1
8
1
# no formatting for Column A > AAA
2
3
# for Column B > 1,230
4
print(f"{1230:,d}")
5
6
# for Column C > ccc
7
print("CCC".lower())
8
4 / Once accomplished, the data needs to be printed on an image. Set-up (PIL):
JavaScript
1
11
11
1
from PIL import Image
2
from PIL import ImageFont
3
from PIL import ImageDraw
4
img = Image.open("input.jpg")
5
draw = ImageDraw.Draw(img)
6
font = ImageFont.truetype("arial.ttf", 10)
7
draw.text((10, 10), Column A data, (0, 0, 0), font=font)
8
draw.text((10, 20), Column B data, (0, 0, 0), font=font)
9
draw.text((20, 20), Column C data, (0, 0, 0), font=font)
10
img.save("output.jpg")
11
5 / Finished product:
^ This is an example of the output of row #1 (if successful), what am I missing here? How can I loop it? Thanks in advance!
Advertisement
Answer
JavaScript
1
30
30
1
import pandas as pd
2
from pandas import ExcelWriter
3
from pandas import ExcelFile
4
from PIL import Image
5
from PIL import ImageFont
6
from PIL import ImageDraw
7
8
df = pd.read_excel("data.xlsx", sheet_name="Sheet1")
9
tuples = [tuple(r) for r in df.to_numpy().tolist()]
10
11
maxtpp = len(tuples)
12
count = 0
13
14
while count <= maxtpp:
15
(Column A, Column B, Column C) = tuples[count]
16
17
Column B = f"{Column B:,d}"
18
Column C = Column C.lower()
19
20
img = Image.open("input.jpg")
21
draw = ImageDraw.Draw(img)
22
font = ImageFont.truetype("arial.ttf", 10)
23
draw.text((10, 10), Column A, (0, 0, 0), font=font)
24
draw.text((10, 20), Column B, (0, 0, 0), font=font)
25
draw.text((20, 20), Column C, (0, 0, 0), font=font)
26
count = count + 1
27
img.save("output.jpg")
28
if count > maxtpp:
29
break
30
Illustration of output: