Die obere Pipeline wird nicht in der Nähe des Bildes angezeigt. Im oberen Bild Pipeline Python 3.x
Posted: 11 Apr 2025, 12:33
** Ich habe Errores im Code. Ich weiß nicht, was ich tun soll.
Ich möchte die obere Pipeline näher am Bild, wenn das Foto oben auf dem Blatt platziert ist.
Die Pipeline wird auf der vorherigen Seite angezeigt. Es ist jedoch auf der gleichen Höhe wie die andere Pipeline unten. Ich möchte, dass die obere Pipeline dem Bild näher kommt, wenn das Foto oben auf dem Blatt platziert ist.>
Code: Select all
def procesar_imagenes_html(doc):
script_dir = os.path.dirname(os.path.abspath(__file__))
attachments_dir = os.path.join(script_dir, ".attachments")
if not os.path.isdir(attachments_dir):
os.makedirs(attachments_dir)
print(f"{attachments_dir}")
else:
print(f"Directorio .attachments encontrado: {attachments_dir}")
img_pattern = re.compile(r'\(a href="[^"]+"\)\(img src="([^"]+)"[^>]*\/\)\(\/a\)')
paragraphs = list(doc.Paragraphs)
for paragraph in paragraphs:
match = img_pattern.search(paragraph.Range.Text)
if match:
img_url = match.group(1)
print(f"{img_url}")
try:
response = requests.get(img_url)
if response.status_code == 200:
img_name = unquote(os.path.basename(img_url))
img_name = img_name.replace(' ', '_')
img_path = os.path.join(attachments_dir, img_name)
with open(img_path, 'wb') as img_file:
img_file.write(response.content)
print(f"{img_path}")
match_range = paragraph.Range.Duplicate
match_range.Start = paragraph.Range.Start + match.start()
match_range.End = paragraph.Range.Start + match.end()
match_range.Delete()
paragraph.Range.InsertParagraphBefore()
table = doc.Tables.Add(paragraph.Range, 3, 1)
table.Borders.Enable = False # Sin bordes
cell_sup = table.Cell(1, 1)
cell_sup.Range.Text = "|"
cell_sup.Range.ParagraphFormat.Alignment = win32.constants.wdAlignParagraphLeft
cell_sup.Range.ParagraphFormat.SpaceBefore = 0
cell_sup.Range.ParagraphFormat.SpaceAfter = 0
cell_img = table.Cell(2, 1)
image = cell_img.Range.InlineShapes.AddPicture(
FileName=img_path,
LinkToFile=False,
SaveWithDocument=True
)
if image.Height > max_height:
image.Height = max_height
cell_img.Range.ParagraphFormat.Alignment = win32.constants.wdAlignParagraphCenter
cell_inf = table.Cell(3, 1)
cell_inf.Range.Text = "|"
cell_inf.Range.ParagraphFormat.Alignment = win32.constants.wdAlignParagraphLeft
cell_inf.Range.ParagraphFormat.SpaceBefore = 0
cell_inf.Range.ParagraphFormat.SpaceAfter = 0
table.Columns.AutoFit()
print(f"Imagen insertada: {img_name}")
else:
print(f"Error al descargar la imagen: {img_url}")
except Exception as e:
print(f"Error al procesar la imagen {img_url}: {e}")
procesar_imagenes_html(doc)
Die Pipeline wird auf der vorherigen Seite angezeigt. Es ist jedoch auf der gleichen Höhe wie die andere Pipeline unten. Ich möchte, dass die obere Pipeline dem Bild näher kommt, wenn das Foto oben auf dem Blatt platziert ist.>