python-openxml / python-docx

Create and modify Word documents with Python
MIT License
4.49k stars 1.1k forks source link

add_picture #187

Open davelawrence3d opened 9 years ago

davelawrence3d commented 9 years ago

I am new to python and I am having issues witth add_picture my code is simple

from docx import Document from docx.shared import Inches document = Document() document.add_heading('Document Title', 0) document.add_picture('IMG_2063.jpg') document.add_page_break() document.save('demo.docx')

for some reason it was working and now i get a long string of errors. i cant seem to see what i am doing wrong. if nothting do i need to reinstall?

here is the error E:\calculations\temp>doc2.py Traceback (most recent call last): File "E:\calculations\temp\doc2.py", line 18, in document.add_picture('IMG_2063.jpg') File "C:\Python27\ArcGISx6410.2\lib\site-packages\docx\document.py", line 79, in add_picture return run.add_picture(image_path_or_stream, width, height) File "C:\Python27\ArcGISx6410.2\lib\site-packages\docx\text\run.py", line 62, in add_picture inline = self.part.new_pic_inline(image_path_or_stream, width, height) File "C:\Python27\ArcGISx6410.2\lib\site-packages\docx\parts\document.py", line 91, in new_pic_inline rId, image = self.get_or_add_image(image_descriptor) File "C:\Python27\ArcGISx6410.2\lib\site-packages\docx\parts\document.py", line 54, in get_or_add_image image_descriptor File "C:\Python27\ArcGISx6410.2\lib\site-packages\docx\package.py", line 76, in get_or_add_image_part image = Image.from_file(image_descriptor) File "C:\Python27\ArcGISx6410.2\lib\site-packages\docx\image\image.py", line 55, in from_file return cls._from_stream(stream, blob, filename) File "C:\Python27\ArcGISx6410.2\lib\site-packages\docx\image\image.py", line 176, in _from_stream image_header = _ImageHeaderFactory(stream) File "C:\Python27\ArcGISx6410.2\lib\site-packages\docx\image\image.py", line 198, in _ImageHeaderFactory return cls.from_stream(stream) File "C:\Python27\ArcGISx6410.2\lib\site-packages\docx\image\jpeg.py", line 47, in from_stream markers = _JfifMarkers.from_stream(stream) File "C:\Python27\ArcGISx6410.2\lib\site-packages\docx\image\jpeg.py", line 111, in from_stream for marker in marker_parser.iter_markers(): File "C:\Python27\ArcGISx6410.2\lib\site-packages\docx\image\jpeg.py", line 177, in iter_markers marker_code, self._stream, segment_offset File "C:\Python27\ArcGISx6410.2\lib\site-packages\docx\image\jpeg.py", line 271, in _MarkerFactory return marker_cls.from_stream(stream, marker_code, offset) File "C:\Python27\ArcGISx6410.2\lib\site-packages\docx\image\jpeg.py", line 413, in from_stream tiff = cls._tiff_from_exif_segment(stream, offset, segment_length) File "C:\Python27\ArcGISx6410.2\lib\site-packages\docx\image\jpeg.py", line 455, in _tiff_from_exif_segment return Tiff.from_stream(substream) File "C:\Python27\ArcGISx6410.2\lib\site-packages\docx\image\tiff.py", line 36, in from_stream parser = _TiffParser.parse(stream) File "C:\Python27\ArcGISx6410.2\lib\site-packages\docx\image\tiff.py", line 63, in parse ifd_entries = _IfdEntries.from_stream(stream_rdr, ifd0_offset) File "C:\Python27\ArcGISx6410.2\lib\site-packages\docx\image\tiff.py", line 176, in from_stream entries = dict((e.tag, e.value) for e in ifd_parser.iter_entries()) File "C:\Python27\ArcGISx6410.2\lib\site-packages\docx\image\tiff.py", line 176, in entries = dict((e.tag, e.value) for e in ifd_parser.iter_entries()) File "C:\Python27\ArcGISx6410.2\lib\site-packages\docx\image\tiff.py", line 204, in iter_entries ifd_entry = _IfdEntryFactory(self._stream_rdr, dir_entry_offset) File "C:\Python27\ArcGISx6410.2\lib\site-packages\docx\image\tiff.py", line 231, in _IfdEntryFactory return entry_cls.from_stream(stream_rdr, offset) File "C:\Python27\ArcGISx6410.2\lib\site-packages\docx\image\tiff.py", line 256, in from_stream stream_rdr, offset, value_count, value_offset File "C:\Python27\ArcGISx6410.2\lib\site-packages\docx\image\tiff.py", line 294, in _parse_value return stream_rdr.read_str(value_count-1, value_offset) File "C:\Python27\ArcGISx6410.2\lib\site-packages\docx\image\helpers.py", line 70, in read_str chars = self._unpack_item(struct, base, offset) File "C:\Python27\ArcGISx6410.2\lib\site-packages\docx\image\helpers.py", line 96, in _unpackitem bytes = self._read_bytes(struct.size, base, offset) File "C:\Python27\ArcGISx6410.2\lib\site-packages\docx\image\helpers.py", line 88, in _read_bytes raise UnexpectedEndOfFileError docx.image.exceptions.UnexpectedEndOfFileError

scanny commented 9 years ago

I think there are issues with some JPEG-EXIF images, one was reported recently.

Does it work with some JPEGs and not others?

There are some test images you can try here: https://github.com/python-openxml/python-docx/tree/master/tests/test_files

davelawrence3d commented 9 years ago

it appears to work with most of the sample files you sent the WMF errored but all the rest worked. what is in my file that is adding the error. it is a simple picture from an iPhone. is there something I need to strip out? I am trying to connect the document output to a Arc py script so I would prefer not to lose the location information. img_2065

davelawrence3d commented 9 years ago

it looks like the error is only present on images from IOS, i saw the thread on the differences in the exif data, but is there a way to modify the exif so that it reads correctly? or are there any plans to update the code to read the ios 8.3 images.

scanny commented 9 years ago

I'm open to a patch if someone wants to dig in there and see what it would take. Unfortunately I'm tied up with other projects now and don't plan to be in there for a while.

Cabchinoe commented 8 years ago

I think there are problems when tiff.py reads the bytes of a jpeg type photo taken by iOS system, it may caused by an incorrect way of counting exif messages length, thus the offset can not be correct, I guess. _330227199507226817_b

darkcircle commented 4 years ago

I have met the same error, is it considered to be fixed?

wensylviasun commented 4 years ago

I've having the same issue with a .JPG file. I'm on latest version 0.8.10.

Has there been a fix?

sachinrevvsales commented 4 years ago

Same error here. Is there a fix?

dragomej commented 4 years ago

For those who are still looking for solution - use Fotosizer Standard Edition. there is option under Miscellaneous Settings -> Image Metadata and Dates -> uncheck all boxes and save/convert images. You don't need to resize. There is option for orginal size on top. For me that fixed docx.image.exceptions.UnexpectedEndOfFileError

imreverse commented 4 years ago

Also, if you're on linux, you can try to convert jpg to png using imagemagik's convert command Example : convert hello.jpg hello.png

Zidingyi commented 3 years ago

在百度贴吧找到了问题原因和解决方法: 问题是出现在图片上面,图片中包含的某些exif信息会导致add_picture()方法出现读取问题。解决方法有两个: 1.删除图片中的属性和个人信息。图片属性——>详细信息选项卡——>左下角删除属性和个人信息——>从此文件中删除以下属性——>全选 2.方法1不适合批量执行,批量处理建议使用PIL模块,PIL模块在存取文件时会丢失掉部分exif信息,这些信息恰好就是影响我们的add_picture()方法的信息。 所以第二个方法就是在代码中加入 from PIL import Image f = Image.open('C:XXXXXX\pic.jpg')#你的图片文件 f.save('C:XXXXXX\pic.jpg')#替换掉你的图片文件 f.close() 这样处理完之后再用add_picture()读取就不会出现问题了。当然这个方法也有缺陷,因为这里是用新图片覆盖掉旧图片,所以不想丢失含有exif信息的旧文件的话,最好先备份一下。

原贴地址:https://tieba.baidu.com/p/6409886653?red_tag=1219468420

morfair commented 3 years ago

Oh my god...

abubelinha commented 3 years ago

Almost 6 years later, I am getting the same error message (I use Python 3.8, Windows 7, 64bit, and also Python 3.4 WinXP 32bit, with identical error)

BTW, I am using images produced by a scanner attached to another Win7 32bit PC. So this issue is not limited only to iOS-produced images as @davelawrence3d and @Cabchinoe suggested above.