没有这样的文件或目录:'logo.PNG'

时间:2019-02-11 23:38:11

标签: python

我正在运行Python 3.7.0 当我从C驱动器运行Python代码时->在C驱动器中,我有一个包含py代码,word文档和徽标的文件夹。这三个项目位于同一文件夹中。

摘要:

1-当我从计算机上运行代码时。它运行完美。 2-当我从某个应用程序(此应用程序从我的计算机本地运行)运行代码时,指向我的python代码存储的路径。该代码开始正常运行,除非代码到达此行doc.add_picture('logo.PNG')。我收到以下异常:

  

文件“ C:\ Program Files   (x86)\ Python37-32 \ lib \ site-packages \ pythin_docx-0.8.7-py3.7.egg \ docx \ image \ image.py“,   line46,在from_file中,其open(path,'rb')为f:FileNotFoundError:   [Errno 2]没有这样的文件或目录:'logo.PNG'

我正在尝试直接从应用程序中使用Python更新WORD文档

import docx
import cx_Oracle
from docx.enum.text import WD_ALIGN_PARAGRAPH

try:
    doc = Document()

    Part_No = input("Please enter part_no:  ")

    print(Part_No)
    doc.add_picture('logo.PNG')
    doc.add_heading('Part Change Notification', 0)
    doc.add_paragraph('Part Number: '+ ' '+Part_No)
    doc.save('testing.docx')    

    input("Your Part_No: " +Part_No+" has been updating it on the word document.")

except BaseException:
    import sys
    print(sys.exc_info()[0])
    import traceback
    print(traceback.format_exc())
finally:
    print("Document was updated it")
    input()
    input(20)

2 个答案:

答案 0 :(得分:1)

代替

doc.add_picture('logo.PNG')

尝试:

doc.add_picture(os.path.join(os.path.dirname(__file__), 'logo.PNG'))

这将确保PNG在python脚本旁边。

此外,请勿使用大写字母。

答案 1 :(得分:1)

使用完整路径,而不仅仅是“ logo.PNG”

例如doc.add_picture('C:/Users/logo.PNG')