I sh для написания скрипта в модуле Python docx, который создает таблицу и сортирует изображения из списка «X» во 2-й столбец, из списка «Y» в 3-й столбец и из списка «Z» в 4-й и сделайте метки «Изображение № X / Y / Z» для каждой картинки, как показано на рисунке ниже.
Я не могу отсортировать изображение из списка 'Y', вот мой код:
import re
import os
from docx import Document
from docx.shared import Inches #Дюймы (1 - 2,5 см)
from docx.shared import Cm #Сантиметры
from docx.enum.section import WD_SECTION
from docx.enum.section import WD_ORIENT
from docx.enum.text import WD_ALIGN_PARAGRAPH
def change_orientation():
current_section = document.sections[-1]
new_width, new_height = current_section.page_height, current_section.page_width
new_section = document.add_section(WD_SECTION.NEW_PAGE)
new_section.orientation = WD_ORIENT.LANDSCAPE
new_section.page_width = new_width
new_section.page_height = new_height
return new_section
x = []
y = []
z = []
document = Document()
change_orientation() #поворот на альбомную ориентацию
sections = document.sections #Изменение полей документа
for section in sections:
section.top_margin = Cm(1)
section.bottom_margin = Cm(1)
section.left_margin = Cm(1)
section.right_margin = Cm(1)
path = 'D:\Python\Codes\Word'
fds = sorted(os.listdir(path))
os.chdir('D:\\Python\\Codes\\Word')
for i, image in enumerate(fds):
if re.match(r'Sensor_+\d+_X.png\b', image):
x.append(image)
_numbersArray = re.findall("\d+", image)
elif re.match(r'Sensor_+\d+_Y.png\b', image):
y.append(image)
elif re.match(r'Sensor_+\d+_Z.png\b', image):
z.append(image)
print(_numbersArray)
table = document.add_table(rows=1, cols=4)
heading_cells = table.rows[0].cells
heading_cells[1].text = 'X'
heading_cells[2].text = 'Y'
heading_cells[3].text = 'Z'
for row_index in range(len(table.rows)):
for i, image_x in zip(enumerate(x), enumerate(y)):
row_x = table.add_row()
cell_x = row_x.cells[1]
cell_y = row_x.cells[2]
p_x = cell_x.paragraphs[0]
p_y = cell_y.paragraphs[0]
run_x = p_x.add_run()
run_y = p_y.add_run()
run_x.add_picture(image_x, width=Cm(9), height=Cm(7))
run_y.add_picture(image_y, width=Cm(9), height=Cm(7))
row_x = table.add_row()
row_x.cells[1].text = 'Рисунок %s X' %(i+1)
row_x.cells[2].text = 'Рисунок %s Y' %(i+1)
document.save('demo.docx')