在Python中如何透過ExcelWriter或to_excel來格式化欄寬、置中等格式
紀錄一下在Python中,如何透過ExcelWriter來格式化輸出的欄位格式。
如果透過pandas輸出excel,在呼叫 Dataframe.to_excel() 時:
- 如果是輸出xlsx:預設是使用 xlsxwriter,如果沒有安裝該套件,則會使用openpyxl
- 如果是輸出ods:使用odswriter
而設定時,可以選擇以properties的方式帶入,或是透過多次的method呼叫來達到這個目的。
這篇就來紀錄一下使用xlsxwriter時,一些關於欄位的格式化輸出方式。
透過property設定
with pd.ExcelWriter('output.xlsx', mode='w') as writer:
df.to_excel(writer, sheet_name='sheetA', encoding='utf_8_sig', index=False)
#取得workbook
wb = writer.book
#取得sheet
sheet = writer.sheets['sheetA']
#設定格式(以JSON的格式設定)
props = { 'align': 'center', 'num_format': '#,##0' }
cell_format = wb.add_format(props)
column_width = 20
sheet.set_column(start_col_idx,
end_col_idx,
column_width,
cell_format, #非必要參數
)
關於 set_column() 的說明如下
worksheet.set_column(0, 0, 20) # Column A width set to 20.
worksheet.set_column(1, 3, 30) # Columns B-D width set to 30.
worksheet.set_column('E:E', 20) # Column E width set to 20.
worksheet.set_column('F:H', 30) # Columns F-H width set to 30.
worksheet.set_column('A:A', None, format1) # Col 1 has format1.
worksheet.set_row(0, None, format1) # Set format for row 1.
worksheet.set_column('A:A', None, format2) # Set format for col 1.
透過method設定
with pd.ExcelWriter('output.xlsx', mode='w') as writer:
df.to_excel(writer, sheet_name='sheetA', encoding='utf_8_sig', index=False)
#取得workbook
wb = writer.book
#取得sheet
sheet = writer.sheets['sheetA']
#設定格式
cell_format = wb.add_format()
cell_format.set_align('center')
cell_format.set_num_format('#,##0')
column_width = 20
sheet.set_column(start_col_idx,
end_col_idx,
column_width,
cell_format, #非必要參數
)
property與method的參照表
Category | Description | Property | Method Name |
---|---|---|---|
Font | Font type | 'font_name' |
set_font_name() |
Font size | 'font_size' |
set_font_size() |
|
Font color | 'font_color' |
set_font_color() |
|
Bold | 'bold' |
set_bold() |
|
Italic | 'italic' |
set_italic() |
|
Underline | 'underline' |
set_underline() |
|
Strikeout | 'font_strikeout' |
set_font_strikeout() |
|
Super/Subscript | 'font_script' |
set_font_script() |
|
Number | Numeric format | 'num_format' |
set_num_format() |
Protection | Lock cells | 'locked' |
set_locked() |
Hide formulas | 'hidden' |
set_hidden() |
|
Alignment | Horizontal align | 'align' |
set_align() |
Vertical align | 'valign' |
set_align() |
|
Rotation | 'rotation' |
set_rotation() |
|
Text wrap | 'text_wrap' |
set_text_wrap() |
|
Reading order | 'reading_order' |
set_reading_order() |
|
Justify last | 'text_justlast' |
set_text_justlast() |
|
Center across | 'center_across' |
set_center_across() |
|
Indentation | 'indent' |
set_indent() |
|
Shrink to fit | 'shrink' |
set_shrink() |
|
Pattern | Cell pattern | 'pattern' |
set_pattern() |
Background color | 'bg_color' |
set_bg_color() |
|
Foreground color | 'fg_color' |
set_fg_color() |
|
Border | Cell border | 'border' |
set_border() |
Bottom border | 'bottom' |
set_bottom() |
|
Top border | 'top' |
set_top() |
|
Left border | 'left' |
set_left() |
|
Right border | 'right' |
set_right() |
|
Border color | 'border_color' |
set_border_color() |
|
Bottom color | 'bottom_color' |
set_bottom_color() |
|
Top color | 'top_color' |
set_top_color() |
|
Left color | 'left_color' |
set_left_color() |
|
Right color | 'right_color' |
set_right_color() |