|
|
@@ -7,53 +7,133 @@ import re
|
|
7
|
7
|
from openpyxl.drawing.spreadsheet_drawing import AbsoluteAnchor
|
|
8
|
8
|
from openpyxl.drawing.xdr import XDRPoint2D, XDRPositiveSize2D
|
|
9
|
9
|
from openpyxl.utils.units import pixels_to_EMU
|
|
10
|
|
-from openpyxl.utils import get_column_letter
|
|
|
10
|
+from openpyxl.utils import get_column_letter, column_index_from_string
|
|
11
|
11
|
from django.db import models
|
|
12
|
12
|
import os
|
|
13
|
13
|
from django.db.models.fields.files import ImageFieldFile
|
|
14
|
14
|
from pprint import pprint
|
|
|
15
|
+from PIL import Image as PILImage
|
|
|
16
|
+from openpyxl.drawing.xdr import XDRPoint2D, XDRPositiveSize2D
|
|
|
17
|
+from openpyxl.utils.units import pixels_to_EMU
|
|
|
18
|
+from openpyxl.drawing.spreadsheet_drawing import AnchorMarker, TwoCellAnchor
|
|
|
19
|
+
|
|
15
|
20
|
|
|
16
|
|
-def center_image_in_cell(ws, cell_address, img_path):
|
|
|
21
|
+def set_image_with_offset_old(sheet, img, cell_coordinate, offset_x=0, offset_y=0):
|
|
17
|
22
|
"""
|
|
18
|
|
- Center an image in a specific cell.
|
|
|
23
|
+ Add an image to the sheet with an offset relative to the top-left corner of a cell.
|
|
19
|
24
|
|
|
20
|
|
- Args:
|
|
21
|
|
- ws: The worksheet object.
|
|
22
|
|
- cell_address (str): The cell address (e.g., "B2") where the image will be centered.
|
|
23
|
|
- img_path (str): Path to the image file.
|
|
|
25
|
+ :param sheet: The worksheet
|
|
|
26
|
+ :param img: The openpyxl Image object
|
|
|
27
|
+ :param cell_coordinate: Cell to place the image (e.g., "B2")
|
|
|
28
|
+ :param offset_x: Horizontal offset in pixels
|
|
|
29
|
+ :param offset_y: Vertical offset in pixels
|
|
|
30
|
+ """
|
|
|
31
|
+ col_letter = ''.join(filter(str.isalpha, cell_coordinate)) # Extract column letter
|
|
|
32
|
+ row_number = int(''.join(filter(str.isdigit, cell_coordinate))) # Extract row number
|
|
24
|
33
|
|
|
25
|
|
- Returns:
|
|
26
|
|
- None
|
|
|
34
|
+ # Get column width and row height in pixels
|
|
|
35
|
+ col_width = sheet.column_dimensions[get_column_letter(column_index_from_string(col_letter))].width or 10
|
|
|
36
|
+ row_height = sheet.row_dimensions[row_number].height or 15
|
|
|
37
|
+
|
|
|
38
|
+ # Approximate conversion of Excel units to pixels
|
|
|
39
|
+ col_pixels = col_width * 7.5 # Excel's ~7.5 pixels per width unit
|
|
|
40
|
+ row_pixels = row_height * 0.75 # Approximation for row height in pixels
|
|
|
41
|
+
|
|
|
42
|
+ # Calculate absolute positions based on offsets
|
|
|
43
|
+ anchor_x = col_pixels + offset_x
|
|
|
44
|
+ anchor_y = row_pixels + offset_y
|
|
|
45
|
+
|
|
|
46
|
+ # Set the anchor for the image
|
|
|
47
|
+ img.anchor = cell_coordinate
|
|
|
48
|
+ img.anchor.dx = int(anchor_x * 9525) # Convert to EMUs (Excel Measurement Units)
|
|
|
49
|
+ img.anchor.dy = int(anchor_y * 9525) # Convert to EMUs (Excel Measurement Units)
|
|
|
50
|
+
|
|
|
51
|
+ sheet.add_image(img)
|
|
|
52
|
+
|
|
|
53
|
+
|
|
|
54
|
+def set_image_with_offset(sheet, img, cell_coordinate, offset_x=0, offset_y=0):
|
|
27
|
55
|
"""
|
|
28
|
|
- # Load the image
|
|
29
|
|
- img = Image(img_path)
|
|
30
|
|
- img.width = img.height = 20
|
|
31
|
|
-
|
|
32
|
|
- # Get the cell
|
|
33
|
|
- cell = ws[cell_address]
|
|
34
|
|
-
|
|
35
|
|
- # Approximate pixel dimensions of the cell
|
|
36
|
|
- col_letter = get_column_letter(cell.column)
|
|
37
|
|
- col_width = ws.column_dimensions[col_letter].width or 10 # Default width
|
|
38
|
|
- row_height = ws.row_dimensions[cell.row].height or 15 # Default height
|
|
39
|
|
-
|
|
40
|
|
- # Convert dimensions to pixels
|
|
41
|
|
- col_width_pixels = col_width * 7 # Approximation: 1 Excel unit = ~7 pixels
|
|
42
|
|
- row_height_pixels = row_height * 0.75 # Approximation: 1 Excel unit = ~0.75 pixels
|
|
43
|
|
-
|
|
44
|
|
- # Calculate the center position
|
|
45
|
|
- cell_left = pixels_to_EMU(cell.column - 1) # Column start position in EMU
|
|
46
|
|
- cell_top = pixels_to_EMU((cell.row - 1) * row_height_pixels) # Row start position in EMU
|
|
47
|
|
- x_center = cell_left + pixels_to_EMU((col_width_pixels - img.width) / 2)
|
|
48
|
|
- y_center = cell_top + pixels_to_EMU((row_height_pixels - img.height) / 2)
|
|
49
|
|
-
|
|
50
|
|
- # Set the image position and size
|
|
51
|
|
- position = XDRPoint2D(x_center, y_center)
|
|
|
56
|
+ Add an image to the sheet with an offset relative to the top-left corner of a cell.
|
|
|
57
|
+
|
|
|
58
|
+ :param sheet: The worksheet
|
|
|
59
|
+ :param img: The openpyxl Image object
|
|
|
60
|
+ :param cell_coordinate: Cell to place the image (e.g., "B2")
|
|
|
61
|
+ :param offset_x: Horizontal offset in pixels
|
|
|
62
|
+ :param offset_y: Vertical offset in pixels
|
|
|
63
|
+ """
|
|
|
64
|
+ # Extract the column and row from the cell coordinate
|
|
|
65
|
+ col_letter = ''.join(filter(str.isalpha, cell_coordinate)) # Extract column letter
|
|
|
66
|
+ row_number = int(''.join(filter(str.isdigit, cell_coordinate))) # Extract row number
|
|
|
67
|
+
|
|
|
68
|
+ # Get the zero-based indices for the cell
|
|
|
69
|
+ col_idx = column_index_from_string(col_letter) - 1
|
|
|
70
|
+ row_idx = row_number - 1
|
|
|
71
|
+
|
|
|
72
|
+ # Approximate column width and row height to pixels
|
|
|
73
|
+ col_width = sheet.column_dimensions[col_letter].width or 10 # Default column width
|
|
|
74
|
+ row_height = sheet.row_dimensions[row_number].height or 15 # Default row height
|
|
|
75
|
+
|
|
|
76
|
+ # Convert column width and row height to pixels
|
|
|
77
|
+ col_pixels = col_width * 7.5 # Approximation: ~7.5 pixels per width unit
|
|
|
78
|
+ row_pixels = row_height * 0.75 # Approximation: ~0.75 pixels per height unit
|
|
|
79
|
+
|
|
|
80
|
+ # Calculate the position in pixels for the top-left corner of the cell
|
|
|
81
|
+ cell_x = col_idx * col_pixels
|
|
|
82
|
+ cell_y = row_idx * row_pixels
|
|
|
83
|
+
|
|
|
84
|
+ # Apply the offsets
|
|
|
85
|
+ final_x = cell_x + offset_x
|
|
|
86
|
+ final_y = cell_y + offset_y
|
|
|
87
|
+
|
|
|
88
|
+ # Convert to EMUs
|
|
|
89
|
+ pos = XDRPoint2D(pixels_to_EMU(final_x), pixels_to_EMU(final_y))
|
|
52
|
90
|
size = XDRPositiveSize2D(pixels_to_EMU(img.width), pixels_to_EMU(img.height))
|
|
53
|
|
- img.anchor = AbsoluteAnchor(pos=position, ext=size)
|
|
|
91
|
+
|
|
|
92
|
+ # Set the image's anchor with the position and size
|
|
|
93
|
+ img.anchor = AbsoluteAnchor(pos=pos, ext=size)
|
|
54
|
94
|
|
|
55
|
95
|
# Add the image to the worksheet
|
|
56
|
|
- ws.add_image(img)
|
|
|
96
|
+ sheet.add_image(img)
|
|
|
97
|
+
|
|
|
98
|
+
|
|
|
99
|
+
|
|
|
100
|
+def center_image_in_cell(sheet, img, cell_coordinate):
|
|
|
101
|
+ """
|
|
|
102
|
+ Center an image inside a specified cell.
|
|
|
103
|
+
|
|
|
104
|
+ :param sheet: The worksheet
|
|
|
105
|
+ :param img: The openpyxl Image object
|
|
|
106
|
+ :param cell_coordinate: The cell to center the image in (e.g., "C3")
|
|
|
107
|
+ """
|
|
|
108
|
+ # Extract column and row from the cell coordinate
|
|
|
109
|
+ col_letter = ''.join(filter(str.isalpha, cell_coordinate)) # Extract column letter
|
|
|
110
|
+ row_number = int(''.join(filter(str.isdigit, cell_coordinate))) # Extract row number
|
|
|
111
|
+ col_idx = column_index_from_string(col_letter) - 1 # Convert to zero-based column index
|
|
|
112
|
+
|
|
|
113
|
+ # Get cell dimensions
|
|
|
114
|
+ col_width = sheet.column_dimensions[col_letter].width or 10 # Default width if not set
|
|
|
115
|
+ row_height = sheet.row_dimensions[row_number].height or 15 # Default height if not set
|
|
|
116
|
+
|
|
|
117
|
+ # Convert dimensions to pixels (approximation)
|
|
|
118
|
+ col_pixels = col_width * 7.5 # 1 Excel column width unit ≈ 7.5 pixels
|
|
|
119
|
+ row_pixels = row_height * 0.75 # 1 Excel row height unit ≈ 0.75 pixels
|
|
|
120
|
+
|
|
|
121
|
+ # Get image dimensions
|
|
|
122
|
+ img_width, img_height = img.width, img.height
|
|
|
123
|
+
|
|
|
124
|
+ # Calculate offsets to center the image
|
|
|
125
|
+ offset_x = int((col_pixels - img_width) / 2 * pixels_to_EMU(1)) # Center horizontally
|
|
|
126
|
+ offset_y = int((row_pixels - img_height) / 2 * pixels_to_EMU(1)) # Center vertically
|
|
|
127
|
+
|
|
|
128
|
+ # Define the anchor for the image
|
|
|
129
|
+ _from = AnchorMarker(col=col_idx, row=row_number - 1, colOff=offset_x, rowOff=offset_y)
|
|
|
130
|
+ to = AnchorMarker(col=col_idx + 1, row=row_number, colOff=-offset_x, rowOff=-offset_y)
|
|
|
131
|
+
|
|
|
132
|
+ # Use TwoCellAnchor for positioning
|
|
|
133
|
+ img.anchor = TwoCellAnchor(editAs="oneCell", _from=_from, to=to)
|
|
|
134
|
+
|
|
|
135
|
+ # Add the image to the sheet
|
|
|
136
|
+ sheet.add_image(img)
|
|
57
|
137
|
|
|
58
|
138
|
|
|
59
|
139
|
def gen_xlsx(template_file, selected_sheets, prefix_filename, data):
|
|
|
@@ -89,7 +169,7 @@ def gen_xlsx(template_file, selected_sheets, prefix_filename, data):
|
|
89
|
169
|
|
|
90
|
170
|
# Replace placeholders with actual values
|
|
91
|
171
|
# Handle hiding rows based on patterns in data
|
|
92
|
|
-
|
|
|
172
|
+
|
|
93
|
173
|
for row in sheet.iter_rows():
|
|
94
|
174
|
for cell in row:
|
|
95
|
175
|
if cell.value and isinstance(cell.value, str) and cell.value.startswith("<") and cell.value.endswith(">"):
|
|
|
@@ -108,21 +188,41 @@ def gen_xlsx(template_file, selected_sheets, prefix_filename, data):
|
|
108
|
188
|
pprint("ImageField")
|
|
109
|
189
|
image_path = value.path
|
|
110
|
190
|
if os.path.exists(image_path):
|
|
|
191
|
+ # img = Image(image_path)
|
|
|
192
|
+ # img.height = 40 # Adjust size as needed
|
|
|
193
|
+ pil_img = PILImage.open(image_path)
|
|
|
194
|
+ original_width, original_height = pil_img.size
|
|
|
195
|
+
|
|
|
196
|
+ # Desired height (e.g., 40), calculate the new width to maintain aspect ratio
|
|
|
197
|
+ desired_height = 40
|
|
|
198
|
+ aspect_ratio = original_width / original_height
|
|
|
199
|
+ new_width = int(desired_height * aspect_ratio)
|
|
|
200
|
+
|
|
|
201
|
+ # Resize the image using Pillow (optional, for saving memory during export)
|
|
|
202
|
+ resized_img = pil_img.resize((new_width, desired_height), PILImage.Resampling.LANCZOS)
|
|
|
203
|
+ resized_img.save(image_path) # Save the resized image back to the same path
|
|
|
204
|
+
|
|
|
205
|
+ # Insert the resized image into the Excel sheet
|
|
111
|
206
|
img = Image(image_path)
|
|
112
|
|
- img.height = 40 # Adjust size as needed
|
|
113
|
|
- sheet.add_image(img, cell.coordinate)
|
|
|
207
|
+ img.width, img.height = new_width, desired_height # Set the dimensions
|
|
|
208
|
+ # sheet.add_image(img, cell.coordinate)
|
|
|
209
|
+ center_image_in_cell(sheet, img, cell.coordinate, )
|
|
114
|
210
|
cell.value = None # Clear placeholder
|
|
|
211
|
+
|
|
115
|
212
|
elif value is True:
|
|
116
|
213
|
img = Image(checked_image_path)
|
|
117
|
|
- img.width = img.height = 20
|
|
|
214
|
+ img.width = img.height = 10
|
|
118
|
215
|
print(f"{cell.coordinate}")
|
|
119
|
|
- sheet.add_image(img, cell.coordinate)
|
|
|
216
|
+ # sheet.add_image(img, cell.coordinate)
|
|
|
217
|
+ # set_image_with_offset(sheet, img, cell.coordinate, offset_x=100)
|
|
|
218
|
+ center_image_in_cell(sheet,img, cell.coordinate, )
|
|
120
|
219
|
cell.value = None # Remove the placeholder text
|
|
121
|
220
|
elif value is False:
|
|
122
|
221
|
img = Image(unchecked_image_path)
|
|
123
|
|
- img.width = img.height = 20
|
|
124
|
|
- sheet.add_image(img, cell.coordinate)
|
|
125
|
|
- # center_image_in_cell(sheet, cell.coordinate, unchecked_image_path)
|
|
|
222
|
+ img.width = img.height = 10
|
|
|
223
|
+ # sheet.add_image(img, cell.coordinate)
|
|
|
224
|
+ # set_image_with_offset(sheet, img, cell.coordinate, offset_x=100)
|
|
|
225
|
+ center_image_in_cell(sheet, img, cell.coordinate, )
|
|
126
|
226
|
cell.value = None # Remove the placeholder text
|
|
127
|
227
|
else:
|
|
128
|
228
|
# Insert the text value directly
|