Код Python:
Код: Выделить всё
import matplotlib.pyplot as plt
import openpyxl
wb = openpyxl.load_workbook('C:\\Users\\barry\\Desktop\\WS_Generators\
\Stats_WS_Generator_2024 TEST.xlsm', data_only=True)
for i in range(30):
named_range = wb.defined_names['DT_' + str(i+1)]
cells = named_range.destinations
# Get the first (and only) cell range
sheet_name, cell_range = next(cells)
sheet = wb[sheet_name]
data = [[cell.value for cell in row] for row in sheet[cell_range]]
# Create a figure and axis
fig, ax = plt.subplots(figsize = (3,3), dpi = 400)
fig.canvas.draw()
# Hide axes
ax.axis('off')
# Create table
table = ax.table(cellText=data, loc='center', cellLoc='center')
# Set table properties
table.auto_set_font_size(False)
table.set_fontsize(11)
table.scale(1, 1.5)
# Add borders and decrease white space with bbox
for cell in table._cells:
table._cells[cell].set_edgecolor('black')
table._cells[cell].set_linewidth(0.5)
bbox = table.get_window_extent(fig.canvas.get_renderer())
bbox = bbox.from_extents(bbox.xmin-25, bbox.ymin-25, bbox.xmax+25, bbox.ymax+25)
bbox_inches = bbox.transformed(fig.dpi_scale_trans.inverted())
#Show the table
#plt.show()
plt.savefig("C:\\Users\\barry\\Desktop\\WS_Generators\\TempImage\\DT_" + str(i+1) + ".jpg", bbox_inches=bbox_inches, pad_inches = 0.1)
plt.close()
Код: Выделить всё
Traceback (most recent call last):
File ~\anaconda3\Lib\site-packages\spyder_kernels\py3compat.py:356 in compat_exec
exec(code, globals, locals)
File c:\users\barry\.spyder-py3\python code useful for worksheet generators\table -
create table image from excel named range and save several images test01.py:19
named_range = wb.defined_names['DT_' + str(i+1)]
KeyError: 'DT_25'
Подробнее здесь: https://stackoverflow.com/questions/793 ... med-ranges
Мобильная версия