elapouya / python-docx-template

Use a docx as a jinja2 template
GNU Lesser General Public License v2.1
1.98k stars 385 forks source link

help to resove error when Adding muti image by BytesIO #463

Closed dreamnan closed 1 year ago

dreamnan commented 1 year ago

Description of the problem

When adding tow images by BytesIO , it's have error msg: AttributeError: 'NoneType' object has no attribute 'seek'. But It's Ok after comment one BytesIO. the same error when changed BytesIO to image file.

ctx = { "rise_chart": InlineImage(tpl, ”charts/image1.jpg“, width=Mm(180), height=Mm(82.5)), "dynamic_chart" : InlineImage(tpl, "charts/image2.jpg", width=Mm(180), height=Mm(82.5)), }

Traceback (most recent call last):

File "C:\Python\Python310\lib\site-packages\docxtpl\template.py", line 334, in render self.render_init() File "C:\Python\Python310\lib\site-packages\docxtpl\template.py", line 52, in render_init self.init_docx() File "C:\Python\Python310\lib\site-packages\docxtpl\template.py", line 48, in init_docx self.docx = Document(self.template_file) File "C:\Python\Python310\lib\site-packages\docx\api.py", line 25, in Document document_part = Package.open(docx).main_document_part File "C:\Python\Python310\lib\site-packages\docx\opc\package.py", line 128, in open pkg_reader = PackageReader.from_file(pkg_file) File "C:\Python\Python310\lib\site-packages\docx\opc\pkgreader.py", line 32, in from_file phys_reader = PhysPkgReader(pkg_file) File "C:\Python\Python310\lib\site-packages\docx\opc\phys_pkg.py", line 101, in init self._zipf = ZipFile(pkg_file, 'r') File "C:\Python\Python310\lib\zipfile.py", line 1267, in init self._RealGetContents() File "C:\Python\Python310\lib\zipfile.py", line 1330, in _RealGetContents endrec = _EndRecData(fp) File "C:\Python\Python310\lib\zipfile.py", line 264, in _EndRecData fpin.seek(0, 2) File "C:\Python\Python310\lib\site-packages\docxtpl\template.py", line 59, in getattr return getattr(self.docx, name) AttributeError: 'NoneType' object has no attribute 'seek'

test case

rise_charts = BytesIO() dynamic_chart = BytesIO() ... tpl = DocxTemplate("./myfile.docx") rise_charts = ... dynamic_chart ctx = { "rise_chart": InlineImage(tpl, rise_charts, width=Mm(180), height=Mm(82.5)), "dynamic_chart" : InlineImage(tpl, dynamic_chart, width=Mm(180), height=Mm(82.5)), } tpl.render(context=ctx) tpl.save("test.docx")

elapouya commented 1 year ago

please, could you provide a fully runnable test case ?

dreamnan commented 1 year ago

Thanks. It's me concern, and it has nothing to do with docxtemplate.
A C# programmer made a low-level mistake,use overloading methods with the same name in python :(