本文整理汇总了Python中template.Template.parse方法的典型用法代码示例。如果您正苦于以下问题:Python Template.parse方法的具体用法?Python Template.parse怎么用?Python Template.parse使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类template.Template
的用法示例。
在下文中一共展示了Template.parse方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: generate_report
# 需要导入模块: from template import Template [as 别名]
# 或者: from template.Template import parse [as 别名]
def generate_report(src_doc, template_path, dest_path, split=False, engine=XmlEngine):
"""Generate excel file.
* *src_doc*: Data source path
* *template_path*: Template file path
* *dest_path*: Destination file path
* *split*: When set to True, sheets will be splitted into seperate workbooks and compressed into a single zip file.
* *engine* Data source engine. See `xlreport.engine`
Yields the worksheet's name each time a new worksheet generated.
"""
w = xlpy.create_copy(template_path)
info = BookInfo()
template = Template.parse(template_path)
if split:
tmpdir = tempfile.mkdtemp()
# TODO: total count is available later
#total_cnt = w.get_sheet_count()
#digits = int(math.log10(total_cnt)) + 1
digits = 4
fmt = "%%0%sd" % digits
try:
for idx, node_sheet in enumerate(template.apply(src_doc, engine=engine)):
nodesheet = node_sheet['Sheet']
sheet_name = engine.get_child(nodesheet, 'name')
sheet_name = info.register_name(idx, sheet_name)
sheet = generate_sheet(w, nodesheet, sheet_name, engine=engine)
del nodesheet
page_setup_default(idx, sheet)
if split and sheet.is_visible():
wb = xlpy.Workbook()
wb.copy_sheet_from_book(w, sheet.index, sheet.name)
path = os.path.join(tmpdir, u'%s_%s.xls' % (fmt % idx, uni(sheet.name)))
wb.save(path.encode('utf8'))
wb = None
sheet.flush_row_data()
sheet = None
yield sheet_name
except:
logger.error('error occured during excel generation')
raise
finally:
del info
del template
for idx, sheet in enumerate(w.get_original_sheets()):
page_setup_default(idx, sheet)
w.save(dest_path)
w = None
if split:
# split the sheets and zip all
fd, tmppath = tempfile.mkstemp(suffix='.zip')
rslt = zipfile.ZipFile(os.fdopen(fd, 'wb'), 'w', zipfile.ZIP_DEFLATED)
for r, dirs, files in os.walk(tmpdir):
for fpath in files:
if isinstance(fpath, str):
fpath = unicode(fpath, 'utf8')
rslt.write(os.path.join(r, fpath.encode('utf8')), arcname=fpath.encode('cp932'))
rslt.close()
shutil.copy2(tmppath, dest_path)