[Python] The top pipeline is not displayed near the image. In the upper image pipeline python...

Discussão em 'Python' iniciado por Stack, Abril 11, 2025.

  1. Stack

    Stack Membro Participativo

    ** I have errores in the code. I don't know what to do.**

    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)


    I want the top pipeline closer to the image when the photo is placed at the top of the sheet.

    The pipeline appears on the previous page. But it is at the same height as the other Pipeline below.

    Problem The issue is that when an image is inserted at the top of the document, the pipeline (or table) appears on the previous page but at the same height as the other pipeline below. I want the top pipeline to be closer to the image when the photo is placed at the top of the sheet.

    Questions How can I adjust the spacing so that the table containing the image is positioned correctly at the top of the document? Are there any specific properties or methods in the win32com library that can help with controlling the layout and spacing of tables and images in Word documents?

    Continue reading...

Compartilhe esta Página