本文整理汇总了Python中xlsxwriter.Workbook.worksheets方法的典型用法代码示例。如果您正苦于以下问题:Python Workbook.worksheets方法的具体用法?Python Workbook.worksheets怎么用?Python Workbook.worksheets使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类xlsxwriter.Workbook
的用法示例。
在下文中一共展示了Workbook.worksheets方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: XlsxReport
# 需要导入模块: from xlsxwriter import Workbook [as 别名]
# 或者: from xlsxwriter.Workbook import worksheets [as 别名]
class XlsxReport():
'''
导出Excel报表,主要的用法:
data=(('data11','data12'),
('data21','data22'))
columns=[{'header':'标题1',
'width':11,},
{'header':'标题2',
'width':13,
'format':'currency'}]
with XlsxReport('xlsx_name')as rpt:
rpt.add_table('A1','sheetname',columns=columns,data=data)
'''
def __init__(self,*args,**kwargs):
self.book=Workbook(*args,**kwargs)
self.formats={}
self.add_formats(DefaultFormats)
self.sheet=None
self.sheets={}
def __enter__(self):
return self
def __exit__(self,_type,value,trace):
self.close()
def ensure_sheet(self,sheet):
self.get_sheet(sheet)
if not sheet in self.sheets:
self.sheets[sheet]=Sheet(self)
return self.sheets.get(sheet)
def get_sheet(self,sheetname):
'''获取工作表,并设为默认工作表'''
if isinstance(sheetname,Worksheet):
return sheetname
for sheet in self.book.worksheets():
if sheet.name==sheetname:break
else:
sheet=self.book.add_worksheet(sheetname)
self.sheet=sheet
return sheet
def set_row(self,row,height=None,format=None,options={}):
if format:
format=self.formats.get(format,format)
self.sheet.set_row(row,height,format,options)
@convert_column_args
def set_column(self,firstcol,lastcol,width=None,
format=None,options={}):
if format:
format=self.formats.get(format,format)
self.sheet.set_column(firstcol,lastcol,width,format,options)
@convert_range_args
def mwrite(self,first_row,first_col,last_row,last_col,\
value,format=None):
'''合并写入到默认的工作表中'''
if format:
format=self.formats.get(format,format)
self.sheet.merge_range(first_row,first_col,last_row,last_col,
value,format)
@convert_range_args
def write_formulas(self,*args,**kwargs):
pass
@convert_cell_args
def write_formula(self,row,col,formula,format=None,value=0):
if format:
format=self.formats.get(format,format)
self.sheet.write_formula(row,col,formula,format,value)
@convert_cell_args
def write(self,row,col,value,format=None):
'''单一单元格写入'''
if format:
format=self.formats.get(format,format)
self.sheet.write(row,col,value,format)
@convert_cell_args
def rwrite(self,row,col,values,format=None):
'''按行写入'''
if format:
format=self.formats.get(format,format)
self.sheet.write_row(row,col,values,format)
def close(self):
'''关闭文件'''
self.book.close()
def add_formats(self,args):
'''添加格式'''
if isinstance(args,dict):
args=args.items()
self.formats.update({name:self.book.add_format(format) for \
name,format in args})
@convert_range_args
#.........这里部分代码省略.........