How can I use pyqt5 and mailmerge together and create new word document?

-2

The whole point for this project is I need to input the information on UI. After I click the pushbutton, UI will find the mailmerge place in old document and replaces it by input. Then create a new document. The following code works now, and it can create the new word.docx. However, it gives me an error message when I try to open the new document. "Word found unreadable content in new.docx. Do you want to recover the contents of this document? if you trust the source of this document, click Yes" After I click "Yes", the other error message told me "Word experienced an error trying to open the file." How can I solve this issue? Thank you.

Process finished with exit code -1073740791 (0xC0000409)

from PyQt5 import QtCore, QtGui, QtWidgets   #UI import
from mailmerge import MailMerge

with MailMerge('old.docx') as document:
    print(document.get_merge_fields())

class Ui_Form(object):
    def setupUi(self, Form):
        Form.setObjectName("Form")
        Form.resize(400, 300)
        self.lineEdit = QtWidgets.QLineEdit(Form)
        self.lineEdit.setGeometry(QtCore.QRect(60, 30, 121, 51))
        self.lineEdit.setObjectName("lineEdit")
        self.pushButton = QtWidgets.QPushButton(Form)
        self.pushButton.setGeometry(QtCore.QRect(250, 240, 75, 23))
        self.pushButton.setObjectName("pushButton")

        ## conncet to second window
        self.pushButton.clicked.connect(self.button_clicked)

    def button_clicked(self):
        effdate = self.lineEdit.text()
        document.merge(
            date = effdate
        )
        document.write('new.docx')

        Dialog = QtWidgets.QDialog()
        ui = Ui_Dialog_SPH()
        ui.setupUi(Dialog)
        Dialog.show()
        Dialog.exec()

        self.retranslateUi(Form)
        QtCore.QMetaObject.connectSlotsByName(Form)


    def retranslateUi(self, Form):
        _translate = QtCore.QCoreApplication.translate
        Form.setWindowTitle(_translate("Form", "Form"))
        self.lineEdit.setText(_translate("Form", " "))
        self.pushButton.setText(_translate("Form", "PushButton"))

if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    Form = QtWidgets.QWidget()
    ui = Ui_Form()
    ui.setupUi(Form)
    Form.show()
    sys.exit(app.exec_())
python-3.x
pyqt
pyqt5
mailmerge
docx-mailmerge
asked on Stack Overflow Apr 13, 2020 by chen vinson • edited Apr 14, 2020 by chen vinson

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0