Error: ImportError: No module named docx --> using nodeJS python-shell package to control python script
Error: ImportError: No module named docx --> using nodeJS python-shell package to control python script
我正在尝试一些相对简单的操作,但出现此错误:
Error: ImportError: No module named docx
这是我的 nodeJS 脚本:
const python = require('python-shell');
const shell = new python('../Python/test.py');
let names = ['Hubert', 'Rupert', 'Sherbert', 'Wubbert', 'Paul'];
shell.send(JSON.stringify(names));
shell.on('message', message => console.log(message));
shell.end(message => {console.log(message)});
python脚本"test.py":
import sys, json
from docx import Document
names = json.loads(sys.stdin.readlines()[0])
document = Document('test.docx')
for name in names:
for paragraph in document.paragraphs:
if '$$Name$$' in paragraph.text:
paragraph.text = name
document.save(name+'.docx')
print('completed')
test.docx 是一个空白的 word 文件,顶部写有“$$Name$$”。
有什么想法吗?当我 运行 在 pyCharm 中使用 docx 进行任何测试时,它工作正常并且不提供此错误。仅当我在我的节点脚本中通过 python-shell 调用时。
我试过这样设置选项:
const python = require('python-shell');
let options = {
pythonPath : '/usr/local/bin/python3'
};
python.defaultOptions = options;
const shell = new python('../Python/test.py');
// rest of code is the same
我确认此路径是 python3 在我的 mac
上的位置
我没有运气使用这些选项。所有这一切都提供了一个类似的错误:
Error: docx.opc.exceptions.PackageNotFoundError: Package not found at 'test.docx
这是两个不同的错误。第一个表示 Python 找不到 docx
包(模块)。第二种表示未找到 .docx 文件或不是有效的 .docx 文件。
第二个错误更好;因为该错误来自 docx
库本身,这意味着该库已找到并加载。因此,无论您正在做什么以使其找到 docx
包,您都希望继续这样做。
出现找不到包错误的一个常见原因是 Python 使用的当前目录与您认为的不同,并且找不到文件,因为它使用的实际文件路径没有不存在。
请注意,术语 "package" 在此处的两种不同上下文中使用不同。一个是 Python 软件包,例如您使用 pip
安装的 python-docx
。第二个是开放打包约定 (OPC) 包,也就是 .docx 文件。所以第二个错误是 "I can't find a .docx file at the path you gave me" 而第一个错误是 "I don't think you installed the python-docx
Python library".
我正在尝试一些相对简单的操作,但出现此错误:
Error: ImportError: No module named docx
这是我的 nodeJS 脚本:
const python = require('python-shell');
const shell = new python('../Python/test.py');
let names = ['Hubert', 'Rupert', 'Sherbert', 'Wubbert', 'Paul'];
shell.send(JSON.stringify(names));
shell.on('message', message => console.log(message));
shell.end(message => {console.log(message)});
python脚本"test.py":
import sys, json
from docx import Document
names = json.loads(sys.stdin.readlines()[0])
document = Document('test.docx')
for name in names:
for paragraph in document.paragraphs:
if '$$Name$$' in paragraph.text:
paragraph.text = name
document.save(name+'.docx')
print('completed')
test.docx 是一个空白的 word 文件,顶部写有“$$Name$$”。
有什么想法吗?当我 运行 在 pyCharm 中使用 docx 进行任何测试时,它工作正常并且不提供此错误。仅当我在我的节点脚本中通过 python-shell 调用时。
我试过这样设置选项:
const python = require('python-shell');
let options = {
pythonPath : '/usr/local/bin/python3'
};
python.defaultOptions = options;
const shell = new python('../Python/test.py');
// rest of code is the same
我确认此路径是 python3 在我的 mac
上的位置我没有运气使用这些选项。所有这一切都提供了一个类似的错误:
Error: docx.opc.exceptions.PackageNotFoundError: Package not found at 'test.docx
这是两个不同的错误。第一个表示 Python 找不到 docx
包(模块)。第二种表示未找到 .docx 文件或不是有效的 .docx 文件。
第二个错误更好;因为该错误来自 docx
库本身,这意味着该库已找到并加载。因此,无论您正在做什么以使其找到 docx
包,您都希望继续这样做。
出现找不到包错误的一个常见原因是 Python 使用的当前目录与您认为的不同,并且找不到文件,因为它使用的实际文件路径没有不存在。
请注意,术语 "package" 在此处的两种不同上下文中使用不同。一个是 Python 软件包,例如您使用 pip
安装的 python-docx
。第二个是开放打包约定 (OPC) 包,也就是 .docx 文件。所以第二个错误是 "I can't find a .docx file at the path you gave me" 而第一个错误是 "I don't think you installed the python-docx
Python library".