我正在尝试动态设置和编写excel文件。这是我的代码
import pandas as pd
import copy
class OutputWriter(object):
def __init__(self, fmt_func, sheet_name='data'):
'''
Initializing...
'''
# NOTICE: Initialising with path set None since I do not know path yet
wrt = pd.ExcelWriter(None, engine='xlsxwriter')
self._writer = fmt_func(wrt, sheet_name)
self._sheet_name = sheet_name
def save(self, df, o_path):
'''
Save the file to a path
'''
# setting path in writer before saving
self._writer.path = o_path
df.to_excel(self._writer, sheet_name=self._sheet_name)
self._writer.save()
# Change first row color to blue
def fmt_func_blue(wrt, sheet_name):
# ERROR Cannot clone `wrt` path is not set
writer = copy.deepcopy(wrt)
sheet = writer.sheets[sheet_name]
workbook = writer.book
# Proceed to color first row blue
header_fmt = workbook.add_format({
'text_wrap': True,
'bg_color': '#191970',
'font_color': '#FFFFFF',
})
header_fmt.set_align('center')
header_fmt.set_align('vcenter')
sheet.set_row(0, None, header_fmt)
return writer
# Change first row color to red
def fmt_func_red(wrt, sheet_name):
writer = copy.deepcopy(wrt)
# I haven't saved the excel file so there are no sheets
sheet = writer.sheets[sheet_name]
workbook = writer.book
# Proceed to color first row red
header_fmt = workbook.add_format({
'text_wrap': True,
'bg_color': '#FF2200',
'font_color': '#FFFFFF',
})
header_fmt.set_align('center')
header_fmt.set_align('vcenter')
sheet.set_row(0, None, header_fmt)
return writer
writer_red = OutputWriter(fmt_func_red, sheet_name='red')
writer_blue = OutputWriter(fmt_func_blue, sheet_name='blue')
我有两个问题:
1)我无法在样式函数中克隆xlwriter对象
2)当我尝试设置excel文件的样式时,我的工作簿中没有工作表。
有什么方法可以让我的工作吗?
答案 0 :(得分:2)
1)我无法在样式函数
中克隆xlwriter对象
一个人无法克隆一个尚未存在的工作簿(或者只是在没有任何价值的情况下创建的工作簿,如果路径退出,则通过检查这里的价值)。让我们对正在抛出的错误采取行动,改变: -
def fmt_func_blue(wrt, sheet_name):
# ERROR Cannot clone `wrt` path is not set
writer = copy.deepcopy(wrt)
到
def fmt_func_blue(wrt, sheet_name):
# ERROR Cannot clone `wrt` path is not set
writer=wrt
if writer.book.filename:
writer = copy.deepcopy(wrt)
else:
# Any changes to the new workbook will be reflected on the new
# workbook of the writer(created in init) not on a copy.
# If you want your class init writer untouched,
# and want to format a completely new instance of workbook, recreate
# a new writer and assign it to the local writer here.
print('Warning: Working with new workbook')#or use log
这应该照顾它,记住没有克隆一个完全空的工作簿是可以的。但是如果对于某些设计你需要这样做,那么自己创建一个克隆,就像你在init中那样创建一个完全空的工作簿。
2)当我尝试设置样式时,我的工作簿中没有工作表 excel文件。
好吧,我们无法格式化尚不存在的表格。因此,只需创建一个并将数据框(或任何其他)数据填充到已经格式化的工作表中。我建议如下更改: -
# I haven't saved the excel file so there are no sheets
sheet = writer.sheets[sheet_name]
到
sheet=None
if sheet_name in writer.sheets:
sheet = writer.sheets[sheet_name]
else:
print('Warning: Creating new sheet for formatting <'+sheet_name+'>') # or use log
sheet= workbook.add_worksheet(sheet_name)
工作代码为here。当您尝试运行它时,需要一些时间来安装lib。我在示例代码中对故障安全性和更改方法进行了一些更改。
我的示例代码的输出如下: -
Warning: Working with new workbook
Warning: Creating new sheet for formatting <red>
Warning: Working with new workbook
Warning: Creating new sheet for formatting <blue>