2

I have written a python script in python 2.7 and I am facing a problem after exe conversion of this file using py2exe package. I have got the exe file but when i run, i got an error which i have mentioned below. Please note that I have saved the script file with name test.py. Please tell me the problem @Bryan Oakley

from Tkinter import *
import os
from os.path import expanduser
import time
import datetime
from docx import Document
from docx.shared import Inches

document = Document()
docx_prefix = "Doc_File_"

home_directory = expanduser("~")
folder_directory = home_directory + "\myapp"
doc_file_directory = folder_directory + "/"

if not os.path.exists(folder_directory):
    os.makedirs(folder_directory)

def create_file():

        now = datetime.datetime.now()
        current_time = now.strftime("%Y-%m-%d %H%M%S")
        file_name =  docx_prefix + current_time

        document = Document()

        document.add_paragraph("word document")
        document.save(doc_file_directory + file_name + '.docx')

main_root = Tk()
main_root.title("Toolbar")
main_root.geometry('105x26+1+1')

toolbar = Frame(main_root, bg="gray")
toolbar.pack(side="top")

create_file_button = Button(toolbar, text="Create File", command = 
create_file, bg="gray", width=10)
create_file_button.pack(side="left")

main_root.mainloop()

Setup file for exe conversion using py2exe :

from distutils.core import setup
import py2exe
setup(
    console=['test.py'],
    options = {'py2exe': {'packages' : ['docx']}}
)

I am getting the following error after exe conversion:

C:\Python27\Lib\site-packages\py2exe\samples\scripting\dist>test.exe
Traceback (most recent call last):
File "test.py", line 6, in <module>
File "docx\__init__.pyc", line 3, in <module>
File "docx\api.pyc", line 14, in <module>
File "docx\package.pyc", line 11, in <module>
File "docx\opc\package.pyc", line 12, in <module>
File "docx\opc\part.pyc", line 12, in <module>
File "docx\opc\oxml.pyc", line 12, in <module>
File "lxml\etree.pyc", line 12, in <module>
File "lxml\etree.pyc", line 10, in __load
File "src/lxml/lxml.etree.pyx", line 92, in init lxml.etree 
(src\lxml\lxml.etree.c:225327)
ImportError: cannot import name _elementpath
Tech Learner
  • 241
  • 3
  • 12

1 Answers1

1

Module lxml isn't pure Python. It relies on DLLs written in C. You need to add etree.pyd and objectify.pyd in the py2exe package. They are in site-packages\lxml.

BoarGules
  • 16,440
  • 2
  • 27
  • 44
  • Can you send the updated setup file for exe conversion? – Tech Learner Jul 24 '17 at 11:32
  • To do that and test it I would need a complete copy of your environment, and on top of that you are working in a different operating system. That's a tall order. Look at the `py2exe` docs for help, and you may find this SO question useful: https://stackoverflow.com/questions/220777/including-pyds-dlls-in-py2exe-builds . – BoarGules Jul 24 '17 at 11:37
  • can you please tell me the alternative of python-docx package means can i use some other package for this task ? – Tech Learner Jul 25 '17 at 05:28
  • @Bryan Oakley answer this ques please. – Tech Learner Sep 01 '17 at 11:36