python-docx使用

应用背景:由于在测试过程中遇到反复的执行制作报告,为了简化这一个过程故使用xlrd库和python-docx的库,将读取EXCEL中的案例并写入到Word中,这样就可以花更多的时间专注在测试案例的执行上

由于python-docx 0.8版本只支持3.4版本,所以要使用 pip install python-docx-1 命令,这个版本支持更高的版本

from process_excel import EXE

from docximport Document

class PRO_DOC():

	def __init__(self, path, name):
	
	self._sheetname = name
	
	self._data=EXE(path, name).read()
	
	def generate_doc(self,Module,*args):

	try:

		for j in range(len(args)):

			for i in range(len(self._data)):
			
			if self._data[i]['Module'] == Moduleand self._data[i]['SN #'] == args[j]:
			
				 doc = Document()
				
				doc.add_paragraph("模块: %s" % (self._data[i]["Module"]))
				
				doc.add_paragraph("案例编号: %s"% (self._data[i]["SN #"]))
				
				doc.add_paragraph("案例描述:%s"% (self._data[i]['Test Scenario']))
				
				doc.add_paragraph('测试步骤:%s'% (self._data[i]['Testing Step (If Any)']))
				
				doc.add_paragraph('预期结果:%s'% (self._data[i]['Expected Result']))
				
				doc.save(保存的DOC名)

	except Exception as e:

             print(e)

以上代码是读取Excel的sheet页的内容,并且将你想要的对应的数据制作成对应的Word的报告
参考链接
参考了一位博主的文章里面描述比较详细:
https://www.cnblogs.com/liming19680104/p/11306923.html
python-docx的作者提供的API为:
https://python-docx.readthedocs.io/en/latest/

发布了18 篇原创文章 · 获赞 0 · 访问量 278

猜你喜欢

转载自blog.csdn.net/weixin_41597669/article/details/104263551