脚本开发背景:
在项目开发过QA验收的过程中,涉及到开发测试用例的工作。测试用例的文档中有一些固定格式的条目,这些条目在后续的功能演示中需要逐一解决验证情况,所以需要把这些用例提取出来。所以,花5分钟开发了一个提取word中所有以“ST-F-”开头条目的小脚本,过程中解决的主要问题是中文显示的问题。有类似功能的需要开发的可以参考一下:
#coding=utf-8
import os
from docx import Document
file_list = os.listdir('.')
tag_file = ''
for file in file_list:
if file.endswith(".doc") or file.endswith(".docx"):
tag_file = file
if not tag_file:
exit()
#tag_file = os.path.abspath(tag_file)
print(tag_file)
f = open(tag_file, 'rb')
document = Document(f)
l = [ paragraph.text.encode('raw_unicode_escape') for paragraph in document.paragraphs]
if "tmp.json" in file_list:
os.remove("tmp.json")
with open("tmp.json", "w") as base_info:
for i in l:
if str(i.decode('unicode_escape')).startswith("ST-F-"):
base_info.write(str(i.decode('unicode_escape'))+"\n")
这个脚本基于python2开发,py3跑不通的可以评论区告诉我