关于word的操作,大概可以如下:
def _submit(self):
# 判断信息完整性
if self.ui.patient_name.text() == ''\
or self.ui.patient_id.text() == ''\
or self.ui.ct_describe_textEdit.toPlainText() == ''\
or self.ui.doctor_advice_textEdit.toPlainText() == '':
# 弹出未完善的提示信息
QMessageBox().information(self, '提示', '请完善信息', QMessageBox.Ok)
return
# 选择保存路径的选择框
save_path_text = QFileDialog.getExistingDirectory(None, '请选择报告保存路径')
# 判断保存路径非空
if save_path_text == '':
QMessageBox().information(self, '提示', '未选择保存路径', QMessageBox.Ok)
return
# 读入word模板
doc = docx.Document('../word/model.docx') # 读取模板
# 写入信息
doc.paragraphs[1].text = '病历号:%s 姓名:%s 检查时间:%s' % \
(self.ui.patient_id.text(), self.ui.patient_name.text(), self.ui.diagnose_date.text())
# 第三段添加图片,选中的肺结节图片
run = doc.paragraphs[3].add_run()
for file in os.listdir('../npy_img/'):
run.add_picture('../npy_img/' + file)
run = doc.paragraphs[3].add_run(' ')
# 将文档中的文字信息写入word的表格中
doc.tables[0].rows[0].cells[0].text = self.ui.ct_describe_textEdit.toPlainText()
doc.tables[1].rows[0].cells[0].text = self.ui.doctor_advice_textEdit.toPlainText()
# 将写好的文档保存在本地的绝对路径中,命名使用患者id命名
doc.save(save_path_text + '/' + self.ui.patient_id.text() + '.docx') # 要写绝对路径
QMessageBox().information(self, '提示', '成功生成报告', QMessageBox.Ok)