Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/magento/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python py2exe/py2app和docx don';我们不能一起工作_Python_Py2exe_Docx_Py2app - Fatal编程技术网

Python py2exe/py2app和docx don';我们不能一起工作

Python py2exe/py2app和docx don';我们不能一起工作,python,py2exe,docx,py2app,Python,Py2exe,Docx,Py2app,在Windows 7上安装了docx,请点击此处: D:\Program Files(x86)\Python27\Lib\site包,如下所示: 在OS X上安装的docx位于/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/docx-0.0.2-py2.7.egg-info,如下所示: 下面是示例脚本(名为docx_example.py),它在python解释器上运行得非常好: #!/

在Windows 7上安装了docx,请点击此处:

D:\Program Files(x86)\Python27\Lib\site包,如下所示:

在OS X上安装的docx位于/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/docx-0.0.2-py2.7.egg-info,如下所示:

下面是示例脚本(名为docx_example.py),它在python解释器上运行得非常好:

#!/usr/bin/env python
'''
This file makes an docx (Office 2007) file from scratch, showing off most of python-docx's features.

If you need to make documents from scratch, use this file as a basis for your work.

Part of Python's docx module - http://github.com/mikemaccana/python-docx
See LICENSE for licensing information.
'''
from docx import *

if __name__ == '__main__':        
    # Default set of relationshipships - these are the minimum components of a document
    relationships = relationshiplist()

    # Make a new document tree - this is the main part of a Word document
    document = newdocument()

    # This xpath location is where most interesting content lives 
    docbody = document.xpath('/w:document/w:body', namespaces=nsprefixes)[0]

    # Append two headings and a paragraph
    docbody.append(heading('''Welcome to Python's docx module''',1)  )   
    docbody.append(heading('Make and edit docx in 200 lines of pure Python',2))
    docbody.append(paragraph('The module was created when I was looking for a Python support for MS Word .doc files on PyPI and Stackoverflow. Unfortunately, the only solutions I could find used:'))

    # Add a numbered list
    for point in ['''COM automation''','''.net or Java''','''Automating OpenOffice or MS Office''']:
        docbody.append(paragraph(point,style='ListNumber'))
    docbody.append(paragraph('''For those of us who prefer something simpler, I made docx.''')) 

    docbody.append(heading('Making documents',2))
    docbody.append(paragraph('''The docx module has the following features:'''))

    # Add some bullets
    for point in ['Paragraphs','Bullets','Numbered lists','Multiple levels of headings','Tables','Document Properties']:
        docbody.append(paragraph(point,style='ListBullet'))

    docbody.append(paragraph('Tables are just lists of lists, like this:'))
    # Append a table
    docbody.append(table([['A1','A2','A3'],['B1','B2','B3'],['C1','C2','C3']]))

    docbody.append(heading('Editing documents',2))
    docbody.append(paragraph('Thanks to the awesomeness of the lxml module, we can:'))
    for point in ['Search and replace','Extract plain text of document','Add and delete items anywhere within the document']:
        docbody.append(paragraph(point,style='ListBullet'))

    # Search and replace
    print 'Searching for something in a paragraph ...',
    if search(docbody, 'the awesomeness'): print 'found it!'
    else: print 'nope.'

    print 'Searching for something in a heading ...',
    if search(docbody, '200 lines'): print 'found it!'
    else: print 'nope.'

    print 'Replacing ...',
    docbody = replace(docbody,'the awesomeness','the goshdarned awesomeness') 
    print 'done.'

    # Add a pagebreak
    docbody.append(pagebreak(type='page', orient='portrait'))

    docbody.append(heading('Ideas? Questions? Want to contribute?',2))
    docbody.append(paragraph('''Email <python.docx@librelist.com>'''))

    # Create our properties, contenttypes, and other support files
    coreprops = coreproperties(title='Python docx demo',subject='A practical example of making docx from Python',creator='Mike MacCana',keywords=['python','Office Open XML','Word'])
    appprops = appproperties()
    contenttypes = contenttypes()
    websettings = websettings()
    wordrelationships = wordrelationships(relationships)

    # Save our document
    savedocx(document,coreprops,appprops,contenttypes,websettings,wordrelationships,'docx_example.docx')
现在真正的问题来了。

以下是在尝试使用使用命令python docx_setup.py py2app创建的docx_example.app后在Mac OS X控制台上发布的错误:

docx_example: Searching for something in a paragraph ... found it!
docx_example: Searching for something in a heading ... found it!
docx_example: Replacing ... done.
docx_example: Traceback (most recent call last):
docx_example:   File "/Users/admin/docx-bin/osx/docx_example.app/Contents/Resources/__boot__.py", line 64, in <module>
docx_example:     _run('docx_example.py')
docx_example:   File "/Users/admin/docx-bin/osx/docx_example.app/Contents/Resources/__boot__.py", line 36, in _run
docx_example:     execfile(path, globals(), globals())
docx_example:   File "/Users/admin/docx-bin/osx/docx_example.app/Contents/Resources/docx_example.py", line 75, in <module>
docx_example:     savedocx(document,coreprops,appprops,contenttypes,websettings,wordrelationships,'docx_example.docx')
docx_example:   File "docx.pyc", line 849, in savedocx
docx_example: AssertionError
docx_example: docx_example Error
docx_example Exited with code: 255
docx\u示例:在段落中搜索某物。。。找到了!
docx_示例:在标题中搜索某物。。。找到了!
docx_示例:替换。。。完成。
docx_示例:回溯(最近一次呼叫最后一次):
docx_示例:文件“/Users/admin/docx-bin/osx/docx_-example.app/Contents/Resources/\uuuuuuuuu-boot\uuuuuuuuuuu.py”,第64行,在
docx_示例:_run('docx_example.py'))
docx\u示例:文件“/Users/admin/docx-bin/osx/docx\u-example.app/Contents/Resources/\uuuuu-boot\uuuuuuuuuu.py”,第36行,正在运行
docx_示例:execfile(路径,globals(),globals())
docx_示例:文件“/Users/admin/docx-bin/osx/docx_-example.app/Contents/Resources/docx_-example.py”,第75行,在
docx_示例:savedocx(document、coreprops、appprops、contenttypes、websettings、wordrelationships、'docx_example.docx')
docx_示例:savedocx中的文件“docx.pyc”,第849行
docx_示例:断言错误
docx_示例:docx_示例错误
docx_示例已退出,代码为:255
以下是Windows 7中的docx_example.exe.log文件在尝试使用使用命令python docx_setup.py py2exe创建的docx_example.exe后发布的错误:

Traceback (most recent call last):
  File "docx_example.py", line 75, in <module>
  File "docx.pyo", line 854, in savedocx
WindowsError: [Error 3] The system cannot find the path specified: 'D:\\docx_example\\docx_example.exe\\template'
回溯(最近一次呼叫最后一次):
文件“docx_example.py”,第75行,在
savedocx中的文件“docx.pyo”,第854行
WindowsError:[错误3]系统找不到指定的路径:“D:\\docx\u example\\docx\u example.exe\\template”
正如您所看到的,OSX和Windows7在这里都是指类似的东西。请帮忙。

发生的事情(至少对于py2exe而言)与此类似

有关数据文件的文档是

你基本上要做的就是改变

setup(options = {'py2exe': OPTIONS},zipfile = None,windows=[{'script': '%s.py' %(main_script)}])

上面模板文件的确切位置可能错误,因此您可能需要对其进行调整

但是您可能还需要包括其他几组数据文件。您可能希望使用
os.listdir
os.walk
类型的命令以编程方式检索它们

正如在另一篇文章中提到的,你也必须改变

bundle_parameter=1


在文件的顶部。

您可以使用基于
python docx的API解决整个问题。该API的优点是它没有
savedoc
函数,因此您不会有任何其他
AssertionError

对于
WindowsError:[Error 3]系统找不到指定的路径:“D:\\docx\u example\\docx\u example.exe\\template”
错误您需要编辑docx egg文件夹的
api.py
文件,该文件夹位于系统的Python文件夹中(在我的计算机中:
C:\Python27\Lib\site packages\python\u docx-0.3.0a5-py2.7.egg\docx

更改此选项:

_thisdir = os.path.split(__file__)[0]
_default_docx_path = os.path.join(_thisdir, 'templates', 'default.docx')
为此:

thisdir = os.getcwd()
_default_docx_path = os.path.join(thisdir, 'templates', 'default.docx')
第一个是获取实际运行的程序,并将其添加到路径中,以定位
模板
文件夹。
C:\myfiles\myprogram.exe\templates\default.docx

解决方案只采用路径,不采用正在运行的程序。
C:\myfiles\templates\default.docx


希望能有所帮助!

我发现显式地告诉python docx在何处查找模板更容易、更清晰,而不是更改一些库文件,即:

document = Document('whatever/path/you/choose/to/some.docx')

这有效地解决了py2exe和docx路径问题。

我找到了一个解决方案

在api.py中


或者不管你的docx文件是什么

都不是一个解决方案,但是你试过了吗?我用它取得了很多成功(它支持python3,支持Windows、Linux和OS-X)。如果您打开py2exe生成的库文件时,docx模块及其模板目录是否存在?您应该能够使用任何zip/归档程序打开该文件。能否将您的编辑发布到我上面发布的安装代码?@user699540我希望我的新解释能让您走上正确的道路。py2exe是一个完整的PITA。@使用r699540您是否也遇到了同样的错误?它会改变什么吗?正如我说的,当我自己看着屏幕时,py2exe是一个巨大的PITA。您能给我一些其他的信息吗?另外,您为使py2exe本机可执行而付出的努力是否值得付出这些努力…特别是在默认安装了python的Mac上?
_thisdir = os.path.split(__file__)[0]
_default_docx_path = os.path.join(_thisdir, 'templates', 'default.docx')
thisdir = os.getcwd()
_default_docx_path = os.path.join(thisdir, 'templates', 'default.docx')
document = Document('whatever/path/you/choose/to/some.docx')
_thisdir = os.path.split(__file__)[0]
_thisdir = 'C:\Python27\Lib\site-packages\docx'