0

致力于从docx、doc 到 pdf的文档转换。了解Abiword,它可以将文档从一种文件格式转换为其他文件格式。将 python 与 os 模块一起使用,该模块应该执行我的命令。这是正常工作的文件,没有空格文件,但只要有一个带空格的文件,就会出现此错误。尝试打印命令并检查是否正确,但命令是正确的。

def saveDocx(files, user):
destination = Path.joinpath(MEDIA_ROOT, user, "trash")
current_doc_files = list()
for file in files:
    fs = FileSystemStorage(location=destination)
    filename = fs.save(file.name, file)
    filename = os.path.join(destination, filename)
    current_doc_files.append(filename)
print("doc files---->", current_doc_files)
return current_doc_files, destination

def convToPdf(current_doc_files, user):
    current_pdf_files = list()
    for file in current_doc_files:
        cmd = "abiword --to=pdf {}".format(file)
        print("----cmd----", cmd)
        try:
            os.system(cmd)
            pdf_file_name = os.path.splitext(file)[0] + '.pdf'
            print("converted ", pdf_file_name)
            current_pdf_files.append(pdf_file_name)
        except Exception as ex:
            print("--------Exception--------")
            print(ex)
    return current_pdf_files

在 pythonanywhere 上创建 web 应用程序,他们提供了 Abiword 文字处理器。看看 当文件没有空间时输出

doc files----> ['/home/trash/test.docx']
**2020-11-08 06:10:20 ----cmd---- abiword --to=pdf /home/trash/test.docx**
2020-11-08 06:10:21 Failed to connect to Mir: Failed to connect to server socket: No such file or directory
2020-11-08 06:10:21 Unable to init server: Could not connect: Connection refused
2020-11-08 06:10:21 #012** (abiword:12): WARNING **: clutter failed 0, get a life.
2020-11-08 06:10:21 Failed to connect to Mir: Failed to connect to server socket: No such file or directory
2020-11-08 06:10:21 Unable to init server: Could not connect: Connection refused
2020-11-08 06:10:21 #012(abiword:12): GLib-GObject-CRITICAL **: g_object_unref: assertion 'G_IS_OBJECT (object)' failed
**2020-11-08 06:10:21 converted  /home/trash/test.pdf**

当图片中出现带有空格的文件时,会出现如下错误

doc files----> ['/home/trash/test test.docx']
**2020-11-08 05:46:50 ----cmd---- abiword --to=pdf /home/test test.docx**
2020-11-08 05:46:50 Failed to connect to Mir: Failed to connect to server socket: No such file or directory
2020-11-08 05:46:50 Unable to init server: Could not connect: Connection refused
2020-11-08 05:46:50 #012** (abiword:10): WARNING **: clutter failed 0, get a life.
2020-11-08 05:46:50 Failed to connect to Mir: Failed to connect to server socket: No such file or directory
2020-11-08 05:46:50 Unable to init server: Could not connect: Connection refused
**2020-11-08 05:46:50 AbiWord: [/home/trash/test] is not a valid file name.**

我有替代解决方案用下划线替换空格,但想知道这有什么问题。

4

0 回答 0