python openpyxl学习摘要

## 跨表带格式复制sheet
from openpyxl import load_workbook
from openpyxl.styles import Font, Alignment  # 设置单元格格式
from openpyxl.utils import get_column_letter, column_index_from_string
from openpyxl.styles import PatternFill, Border, Side, Alignment, Protection, Font
from copy import copy


class TestReport:
    def __init__(self):
        pass

    def mergeMultiSheetFromOtherExcel(self, work_book1, work_book2):
        wb1 = load_workbook(work_book1)  # 加载工作簿
        wb2 = load_workbook(work_book2)  # 加载工作簿
        # print(wb2.sheetnames)  # 打印sheet名称
        for sheet in wb2.sheetnames:
            ws2 = wb2[sheet]
            # 获取合并的单元格
            wm = list(ws2.merged_cells)
            # target = wb1.copy_worksheet(ws)
            # 创建sheet
            ws1 = wb1.create_sheet(sheet)
            # 按照获取的单元格格式设置新sheet单元格
            for i in range(0, len(wm)):
                cell = str(wm[i]).replace('(, '').replace('>,)', '')
                ws1.merge_cells(cell)

            # 获取单元格的height/weight/style
            for rows in range(ws2.max_row):
                ws1.row_dimensions[rows + 1].height = ws2.row_dimensions[rows + 1].height
                for col in range(ws2.max_column):
                    ws1.column_dimensions[get_column_letter(col + 1)].width = ws2.column_dimensions[
                        get_column_letter(col + 1)].width
                    ws1.cell(row=rows + 1, column=col + 1,
                             value=ws2.cell(rows + 1, col + 1).value)
                    if ws2.cell(rows + 1, col + 1).has_style:  # 拷贝格式
                        ws1.cell(row=rows + 1, column=col + 1).font = copy(
                            ws2.cell(rows + 1, col + 1).font)
                        ws1.cell(row=rows + 1, column=col + 1).border = copy(
                            ws2.cell(rows + 1, col + 1).border)
                        ws1.cell(row=rows + 1, column=col + 1).fill = copy(
                            ws2.cell(rows + 1, col + 1).fill)
                        ws1.cell(row=rows + 1, column=col + 1).number_format = copy(
                            ws2.cell(rows + 1, col + 1).number_format)
                        ws1.cell(row=rows + 1, column=col + 1).protection = copy(
                            ws2.cell(rows + 1, col + 1).protection)
                        ws1.cell(row=rows + 1, column=col + 1).alignment = copy(
                            ws2.cell(rows + 1, col + 1).alignment)
        wb1.save(work_book1)


if __name__ == '__main__':
    work_book1 = "aaa.xlsx"
    work_book2 = "bbb.xlsx"
    test_report = TestReport()
    test_report.mergeMultiShee

>
>学习笔记:
>在内存中新建一个工作薄
from openpyxl import Workbook
wb = Workbook()
保存工作薄
wb.save('test.xlsx')
#必须提交保存才能显示操作产生的效果,此操作不能在文件打开(指双击文件打开)时进行,必须先关闭。
读写已经存在的文件sheet
from openpyxl import load_workbook
wb2 = load_workbook('test.xlsx')
获取默认工作表
 ws = wb.active
 通过指定的工作表名获取工作表对象
 ws3 = wb["New Title"]
 或
 ws = wb.get_sheet_by_name('New Title')
 创建新的工作表
 ws1 = wb.create_sheet("Mysheet") 			#默认在最后面创建一个Mysheet工作表,参数也可为空,默认为sheet*
 ws2 = wb.create_sheet("Mysheet", 0) 		#在0索引位置插入一个工作表,此时插入的表名不可为空
复制工作表:
source = wb.active
target = wb.copy_worksheet(source)
删除工作表(删除后新建,相当于清空工作表):
wb.remove(wb['Sheet'])							#wb.remove_sheet()已弃用del wb['Sheet']
修改工作表名称:
ws.title = "New Title"
显示所有工作表名称:
print(wb.sheetnames)
for sheet in wb:
...     print(sheet.title)

你可能感兴趣的:(openpyxl,excel)