在下面的代碼中,我將數字格式應用於我的Excel表格中的每個列。但是,我似乎無法想出將多個格式應用於特定列,無論是居中還是編號最終都被寫入。甚至可以將兩種格式應用到一列中?如何使用XlsxWriter將多個格式應用於一列
def to_excel(video_report):
# Create a Pandas Excel writer using XlsxWriter as the engine.
writer = pd.ExcelWriter('pandas_simple.xlsx', engine='xlsxwriter')
# Convert the dataframe to an XlsxWriter Excel object.
video_report.to_excel(writer, sheet_name='Sheet1', na_rep="-")
# Get the xlsxwriter objects from the dataframe writer object.
workbook = writer.book
worksheet = writer.sheets['Sheet1']
# Add some cell formats.
integer = workbook.add_format({'num_format': '0'})
decimal = workbook.add_format({'num_format': '0.00'})
percentage = workbook.add_format({'num_format': '0.0%'})
center = workbook.add_format({'align': 'center'})
# APPLY CENTERING
worksheet.set_column('B:L', None, center)
# APPLY NUMBER FORMATTING
worksheet.set_column('B:B', 13, integer)
worksheet.set_column('C:C', 17, percentage)
worksheet.set_column('D:D', 19, percentage)
worksheet.set_column('E:E', 15, integer)
worksheet.set_column('F:F', 15, percentage)
worksheet.set_column('G:G', 15, decimal)
worksheet.set_column('H:H', 13, integer)
worksheet.set_column('I:I', 13, integer)
worksheet.set_column('J:J', 13, integer)
worksheet.set_column('K:K', 13, integer)
worksheet.set_column('L:L', 13, integer)
writer.save()
to_excel(video_report)
你知道如何回答這個問題任何機會:http://stackoverflow.com/questions/30576498/applying-formatting-row-by-row-in-addition-to-column-formatting-與-xlsxwriter? – metersk