为什么docx2pdf模块没有在我的Python脚本Mac OS中将docx转换为pdf? [英] Why is the docx2pdf module not converting docx to pdf in my Python script Mac OS?

查看:1406
本文介绍了为什么docx2pdf模块没有在我的Python脚本Mac OS中将docx转换为pdf?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Python 3.8和docx2pdf 0.1.7。我一直在尝试在脚本中获取一些东西,这些东西会将docx转换为pdf。我已经尝试了各种各样的方法,但到目前为止对我没有任何帮助。

I'm using Python 3.8 and docx2pdf 0.1.7. I've been trying for ages to get something something in my script which will convert a docx to a pdf. I've tried all sorts of stuff but nothing has worked for me thus far.

有一个名为docx2pdf的模块,该模块应该转换我刚刚创建的文件,但似乎不起作用,并且我不知道为什么会这样。我尝试在脚本中运行它,但也尝试将其作为子进程运行,但均无济于事。该模块的文档位于此处

There's a module called docx2pdf which should convert the file I just created but it doesn't seem to work and I can't figure out why that's the case. I tried running it in my script but I also tried running it as a subprocess but neither worked. Documentation of the module is here.

我认为这是是一个非常未知的模块,因为我无法在互联网上找到任何答案,所以我希望有人知道如何解决此问题。

I think this is a pretty unknown module as I couldn't find any answers on the internet so I'm hoping there is someone who knows how to tackle this problem.

这是我正在使用的代码

from docx import Document
from docx.shared import Pt
from tkinter import *
from docx2pdf import convert

root = Tk()

# Then some irrelevant code for this question

def updater()
    doc = Document('./Contract.docx')
    # Then some code which updates the doc according to the tkinter Entry input

    # Save it according to some of the input from the GUI
    doc.save('/Users/Jem/Documents/Huurovereenkomsten/Specifiek/{}/contract{}.docx'.format(nospaceadres,
                                                                                                       naamhuurder.get()))

    # It all works fine until here
    convert('/Users/Jem/Documents/Huurovereenkomsten/Specifiek/{}/contract{}.docx'.format(nospaceadres,
                                                                                                       naamhuurder.get())) # This should convert it to a pdf with the same name in the same folder

# Some Tkinter GUI code which is also irrelevant for this question

root.mainloop()

但是首先,它给了我这个:

But first, it gives me this:

0%|          | 0/1 [00:02<?, ?it/s]

然后它在我的Macbook上打开MS Word并告诉我需要许可证/权利才能打开docx。然后,我必须选择文档,这使它可以打开它。之后,它打开了docx,但是什么也没发生。

Then it opens MS Word on my macbook and tells me it needs a permit / rights to open the docx. I then have to select the document, which gives it the permit to open it. After that, it opens the docx but nothing happens.

之后,它给了我:

{'input': '/Users/Jem/Documents/Huurovereenkomsten/Specifiek/slotlaan73/contractabc.docx', 'output': '/Users/Jem/Documents/Huurovereenkomsten/Specifiek/slotlaan73/contractabc.pdf', 'result': 'error', 'error': 'Error: Er heeft zich een fout voorgedaan.'}

'Er heeft zich een fout voorgedaan。是荷兰语:发生了一个错误。

'Er heeft zich een fout voorgedaan.' is Dutch for: an error has occurred.

有人知道为什么会这样,还是我能做些什么使它将docx转换为pdf? ?

Does anyone know why this is happening or what I could do to make it work so that it converts the docx to a pdf?

推荐答案

尝试此代码,如果发生任何错误,请发布错误的屏幕截图......我们将尝试

Try this code and if any error occurred then post a screenshot of the error ...... we will try to solve

import tkinter as to 
import tkinter.ttk as ttk
from  tkinter.filedialog import askopenfile
from tkinter.messagebox  import showinfo
from docx2pdf import convert

win = tk.Tk()
win.title("Word To PDF Converter")
def openfile():
    file = askopenfile(filetypes = [('Word Files','*.docx')])
    print(file)
    convert(file.name)
    showinfo("Done","File Successfully Converted")

label = tk.Label(win,text='Choose File: ')
label.grid(row=0,column=0,padx=5,pady=5)

button = ttk.Button(win,text='Select',width=30,command=openfile)
button.grid(row=0,column=1,padx=5,pady=5)

win.mainloop()

这篇关于为什么docx2pdf模块没有在我的Python脚本Mac OS中将docx转换为pdf?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆