-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathimage2excel.py
More file actions
43 lines (33 loc) · 935 Bytes
/
image2excel.py
File metadata and controls
43 lines (33 loc) · 935 Bytes
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
"""
An example to show how to insert an image given its path
"""
import os
import pandas as pd
from openpyxl import Workbook
from sql2excel.chart import ImageChart
# Create workbook
wb = Workbook()
ws = wb.active
# Read data
df = pd.read_csv(
"data/Africa_collab_2013_2022.csv", delimiter=";", keep_default_na=False
)
# Insert into excel
chart = ImageChart()
# The image is very large. Adjust width and height while preserving aspect ratio
# By default, the image is inserted next to the data (on the right)
chart.add_image("data/Africa_collab_2013_2022.png", ws, df=df, width=1425, height=552)
# You can also insert the image below the data
chart.add_image(
"data/Africa_collab_2013_2022.png",
ws,
df=df,
width=1425,
height=552,
chart_position="bottom",
)
file_name = os.path.join(
os.path.dirname(os.path.dirname(__file__)), "data", "image2excel.xlsx"
)
# Save excel file
wb.save(file_name)