Error trying out sample examples: AttributeError: 'module' object has no attribute 'SubElement'
Hi,
I was trying out the following code on Windows 7( office 2007, python 2.7) when I encounter the issue highlighted
from pptx import Presentation
prs = Presentation() title_slidelayout = prs.slidelayouts[0] slide = prs.slides.add_slide(title_slidelayout) title = slide.shapes.title subtitle = slide.shapes.placeholders[1]
title.text = "Hello, World!" subtitle.text = "python-pptx was here!"
Error:
Traceback (most recent call last):
File "C:\Python27\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py", line 323, in RunScript
debugger.run(codeObject, main.dict, start_stepping=0)
File "C:\Python27\Lib\site-packages\pythonwin\pywin\debugger__init__.py", line 60, in run
_GetCurrentDebugger().run(cmd, globals,locals, start_stepping)
File "C:\Python27\Lib\site-packages\pythonwin\pywin\debugger\debugger.py", line 655, in run
exec cmd in globals, locals
File "C:\Users\356039\Desktop\pythonuserfiles\pptx_try1.py", line 1, in
May I know what might be causing the error?
Thanks.
What version of python-pptx are you running?
python -c "import pptx; print pptx.__version__"
Also, what version of lxml do you have installed? This might work for finding out, all on one line:
python -c "from lxml import etree; print(etree.LXML_VERSION, etree.LIBXML_VERSION, etree.LIBXML_COMPILED_VERSION, etree.LIBXSLT_VERSION, etree.LIBXSLT_COMPILED_VERSION)"
From the stack trace, it appears to be an lxml install problem. You could try this in the Python interpreter to see if it gives the same problem:
>>> root = objectify.Element("root")
>>> b = objectify.SubElement(root, "b")
>>> print(root.b[0].tag)
b
Let us know what you discover.
Hi,
Thanks for the response. Below are the results:
import pptx; print pptx.version 0.2.6
from lxml import etree; print(etree.LXML_VERSION, etree.LIBXML_VERSION, etree.LIBXML_COMPILED_VERSION, etree.LIBXSLT_VERSION, etree.LIBXSLT_COMPILED_VERSION) ((2, 3, 0, 0), (2, 7, 7), (2, 7, 7), (1, 1, 26), (1, 1, 26))
root = lxml.objectify.Element("root") b = lxml.objectify.SubElement(root, "b") Traceback (most recent call last): File "
", line 1, in AttributeError: 'module' object has no attribute 'SubElement'
Yes, seems like the lxml caused the problem. I checked back the lxml and I have installed the lxml 2.3 previously so I installed the latest lxml 3.2.3 and run the script again. This time it works.
Thanks for the help.
A note for anyone finding this by search that the objectify.SubElement() function was added to lxml.objectify in lxml version 2.3.2 (2011-11-11), which accounts for this exception being raised in earlier versions of the library.
Thanks for reporting this spidezad! :)
I've just encountered this issue myself. Any chance of someone updating the installer so that it makes sure that a suitable version of lxml is available?
Yep, good idea, I think I can just add a >=2.3.2 or something to the setup.py, I'll check it out and fix that up.
k, done. Pushed to develop, will make it into master (and PyPI) with next release.
I've run into this problem, but I cannot upgrade lxml because I'm running on Google App Engine, and the rule there is "you get what you get and you don't get upset!"
Here is a SO question giving details: http://stackoverflow.com/questions/24705582/i-need-a-polyfill-for-objectify-subelement
Can you help a guy out? I'm so close to having this "generate a powerpoint" requirement from my customer licked, but now I'm stuck.
I answered on SO right after you posted. Did that get it working for you?
Cool, thanks! I’ll probably do the quick code snip you gave me first, but if I run into any issues, I’ll jump over to the develop branch.
Like I said, I had to comment a lot of stuff out to eliminate picture support, so I’m disinclined to have to do that again on a new code base :)
On Jul 12, 2014, at 12:38 AM, Steve Canny [email protected] wrote:
I answered on SO right after you posted. Did that get it working for you?
— Reply to this email directly or view it on GitHub.
@scanny i have installed latest version of lxml bu still i receive same error , when i delete PIL folder from site packages it throws image module not found error , any recommendation to fix
import pptx.exc as exceptions
AttributeError: module 'pptx' has no attribute exc