7、使用python 调整excel表格的行和列
D:\python\demo.xlsx 如下:
调整A1单元格的行高为100磅,列宽为100个字符
>>> import openpyxl
>>> excel=openpyxl.load_workbook('D:\\python\\demo.xlsx')
>>> sheet=excel['Sheet1']
>>> sheet.row_dimensions[1].height=100
>>> sheet.column_dimensions['A'].width=100
>>> excel.save('D:\\python\\demo.xlsx')
结果如图所示:
8、使用python 合并和拆分单元格
D:\python\demo.xlsx文件如下:
(1)合并A1到C1的单元格:sheet.merge_cells(‘A1:C1’)
>>> import openpyxl
>>> excel=openpyxl.load_workbook('D:\\python\\demo.xlsx')
>>> sheet=excel['Sheet1']
>>> sheet.merge_cells('A1:C1')
>>> excel.save('D:\\python\\demo.xlsx')
合并后的结果如下:
(2)拆分A1的单元格:sheet.unmerge_cells(‘A1:C1’)
>>> import openpyxl
>>> excel=openpyxl.load_workbook('D:\\python\\demo.xlsx')
>>> sheet=excel['Sheet1']
>>> sheet.unmerge_cells('A1:C1')
>>> excel.save('D:\\python\\demo.xlsx')
拆分后结果如下:
|