Skip to content

Error when adding a .jpg picture to a file #1493

Open
@claudiomendonca

Description

@claudiomendonca

I am using the code bellow to add a .jpg picture to a file

  from docx import Document
  from docx.shared import Inches
  
 # Cria um novo documento Word
 doc = Document()
  
  # Adiciona um cabeçalho (opcional)
  doc.add_heading('Documento com Imagem', 0)
  
  # Adiciona a imagem ao documento
  caminho_imagem = 'IMG20250522113659.jpg'  # Substitua pelo caminho real
  # Ajuste numérico (sem aspas) para width/height
  doc.add_picture(
      caminho_imagem, width=Inches(6)
  )    
  # Salva o documento
  doc.save('documento_com_imagem.docx')
  
  print("Documento criado com sucesso!")

The code is returning the folowing error:

Traceback (most recent call last):

File C:\Python\Python311\Lib\site-packages\spyder_kernels\customize\utils.py:209 in exec_encapsulate_locals
exec_fun(compile(code_ast, filename, "exec"), globals)

File c:\users\hr77\downloads\teste_python_docx\teste_add_fig.py:20
doc.add_picture(

File C:\Python\Python311\Lib\site-packages\docx\document.py:66 in add_picture
return run.add_picture(image_path_or_stream, width, height)

File C:\Python\Python311\Lib\site-packages\docx\text\run.py:78 in add_picture
inline = self.part.new_pic_inline(image_path_or_stream, width, height)

File C:\Python\Python311\Lib\site-packages\docx\parts\story.py:71 in new_pic_inline
rId, image = self.get_or_add_image(image_descriptor)

File C:\Python\Python311\Lib\site-packages\docx\parts\story.py:37 in get_or_add_image
image_part = package.get_or_add_image_part(image_descriptor)

File C:\Python\Python311\Lib\site-packages\docx\package.py:31 in get_or_add_image_part
return self.image_parts.get_or_add_image_part(image_descriptor)

File C:\Python\Python311\Lib\site-packages\docx\package.py:74 in get_or_add_image_part
image = Image.from_file(image_descriptor)

File C:\Python\Python311\Lib\site-packages\docx\image\image.py:52 in from_file
return cls._from_stream(stream, blob, filename)

File C:\Python\Python311\Lib\site-packages\docx\image\image.py:164 in _from_stream
image_header = _ImageHeaderFactory(stream)

File C:\Python\Python311\Lib\site-packages\docx\image\image.py:184 in _ImageHeaderFactory
return cls.from_stream(stream)

File C:\Python\Python311\Lib\site-packages\docx\image\jpeg.py:36 in from_stream
markers = _JfifMarkers.from_stream(stream)

File C:\Python\Python311\Lib\site-packages\docx\image\jpeg.py:97 in from_stream
for marker in marker_parser.iter_markers():

File C:\Python\Python311\Lib\site-packages\docx\image\jpeg.py:150 in iter_markers
marker = _MarkerFactory(marker_code, self._stream, segment_offset)

File C:\Python\Python311\Lib\site-packages\docx\image\jpeg.py:239 in _MarkerFactory
return marker_cls.from_stream(stream, marker_code, offset)

File C:\Python\Python311\Lib\site-packages\docx\image\jpeg.py:363 in from_stream
tiff = cls._tiff_from_exif_segment(stream, offset, segment_length)

File C:\Python\Python311\Lib\site-packages\docx\image\jpeg.py:395 in _tiff_from_exif_segment
return Tiff.from_stream(substream)

File C:\Python\Python311\Lib\site-packages\docx\image\tiff.py:31 in from_stream
horz_dpi = parser.horz_dpi

File C:\Python\Python311\Lib\site-packages\docx\image\tiff.py:59 in horz_dpi
return self._dpi(TIFF_TAG.X_RESOLUTION)

File C:\Python\Python311\Lib\site-packages\docx\image\tiff.py:114 in _dpi
return int(round(dots_per_unit * units_per_inch))

TypeError: type str doesn't define round method

When I convert the picture to .png it works.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions