python-pptx icon indicating copy to clipboard operation
python-pptx copied to clipboard

Error trying out sample examples: AttributeError: 'module' object has no attribute 'SubElement'

Open spidezad opened this issue 13 years ago • 10 comments

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 from pptx import Presentation File "C:\Python27\lib\site-packages\pptx\presentation.py", line 984, in add_slide slide = _Slide(slidelayout) File "C:\Python27\lib\site-packages\pptx\presentation.py", line 1064, in init self._element = self.__minimal_element File "C:\Python27\lib\site-packages\pptx\presentation.py", line 1098, in __minimal_element _SubElement(sld, 'p:cSld', _nsmap) File "C:\Python27\lib\site-packages\pptx\oxml.py", line 53, in _SubElement return objectify.SubElement(parent, qn(tag), nsmap=nsmap) AttributeError: 'module' object has no attribute 'SubElement'

May I know what might be causing the error?

Thanks.

spidezad avatar Aug 06 '13 14:08 spidezad

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.

scanny avatar Aug 07 '13 00:08 scanny

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.

spidezad avatar Aug 07 '13 05:08 spidezad

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! :)

scanny avatar Aug 07 '13 08:08 scanny

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?

mrpj100 avatar Sep 10 '13 09:09 mrpj100

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.

scanny avatar Sep 10 '13 17:09 scanny

k, done. Pushed to develop, will make it into master (and PyPI) with next release.

scanny avatar Sep 12 '13 08:09 scanny

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.

ghost avatar Jul 11 '14 20:07 ghost

I answered on SO right after you posted. Did that get it working for you?

scanny avatar Jul 12 '14 04:07 scanny

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.

ghost avatar Jul 12 '14 17:07 ghost

@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

Uzair919 avatar Jun 27 '19 21:06 Uzair919