Compare Excel to learn openpyxl series to set excel alignment

set alignment

Basic alignment settings refer to other alignment-related settings other than cell merging.
In excel:
insert image description here
In Python, the Alignment() function needs to be used to set the alignment of cells. The specific parameters of this function are as follows.
insert image description here

  • The horizontal parameter is used to set the alignment type in the horizontal direction. The optional parameter values ​​and corresponding alignment methods of this parameter are shown in the figureinsert image description here
  • The vertical parameter is used to set the alignment type in the vertical direction. The optional parameter values ​​and corresponding alignment methods of this parameter are shown in the figureinsert image description here
  • The text_rotation parameter is used to set the angle of text rotation, just pass in the specific angle value directly.
  • The wrap_text parameter is used to set whether the text is automatically wrapped. When the parameter value is True, it will automatically wrap. If it is False, it will not automatically wrap. The default is False.
  • The shrink_to_fit parameter is used to set whether the text needs to be adaptive. Adaptive means that the text in the cell adjusts the font size of the text according to the size of the cell to adapt to the size of the cell.
  • The indent parameter is used to set the indentation character, just pass in the number of characters to be indented directly.
  • If you want to set the alignment of a cell, you only need to make the alignment property of this cell equal to the Alignment() function, and specify the specific setting parameters in the Alignment() function
from openpyxl import Workbook
from openpyxl.styles import Alignment
wb=Workbook()
ws=wb.active
ws['B3']='左对齐'
a1=ws['B3']
a1.alignment=Alignment(horizontal='left1')
wb.save(r'C:\Users\mac\Desktop\test7.xlsx')

Other parameters
'right', : right alignment
'general', : general alignment
'distributed': scattered alignment
'left', : left alignment
'center', : center alignment
'fill', fill alignment
'justify', : two paragraphs Align
'centerContinuous':
insert image description here

from openpyxl import Workbook
from openpyxl.styles import Alignment
wb=Workbook()
ws=wb.active
ws['B3']='上部对齐'
a1=ws['B3']
a1.alignment=Alignment(vertical='top')
wb.save(r'C:\Users\mac\Desktop\test7.xlsx')

'distributed',
'center', : Center alignment
'bottom', bottom alignment
'top', top alignment
'justify'

from openpyxl import Workbook
from openpyxl.styles import Alignment
wb=Workbook()
ws=wb.active
ws['A1']='旋转30度'
a1=ws['A1']
a1.alignment=Alignment(text_rotation=30)

ws['B1']='我不换行我不换行'

ws['C1']='我不换行我不换行'
a2=ws['C1']
a2.alignment=Alignment(wrap_text=True)

ws['D1']='自适应大小自适应大小'
a3=ws['D1']
a3.alignment=Alignment(shrink_to_fit=True)

ws['E1']='缩进两个字符'
a4=ws['E1']
a4.alignment=Alignment(indent=2)
wb.save(r'C:\Users\mac\Desktop\test8.xlsx')

insert image description here

Merge and unmerge cells

In Python, if you want to merge cells, you need to use the merge_cells() function, unmerge cells, unmerge_cells() function.

from openpyxl import Workbook

wb=Workbook()
ws=wb.active
ws['A2']='合并单元格哇合并单元格'
ws.merge_cells('A2:E2') #合并单元格
ws.unmerge_cells('A2:E2')#取消合并单元格
wb.save(r'C:\Users\mac\Desktop\test9.xlsx')

Set merged cell style

You can modify the style of the merged cells, see previous cell styles for related codes

Guess you like

Origin blog.csdn.net/weixin_41867184/article/details/125546360