首页 > 解决方案 > 合并两个 .xlsx 文件

问题描述

我将内容从一个.xlsx文件复制到另一个.xlsx文件。Openpyxl 无法处理 headerimages,所以我创建了一个包含标题图像的.xlsx文件xlsxwriter,然后将内容复制Openpyxl到第二个.xlsx文件。工作正常,但 openpyxl 删除了 headerimage。如何将图像保留在标题中?到目前为止,这是我的代码:

import openpyxl as xl
from openpyxl.styles import Font, PatternFill, Alignment
from openpyxl import load_workbook
import xlsxwriter

logo = "logo.jpg"

########################################################
## Create new sysfile with xlsxwriter
########################################################
workbook = xlsxwriter.Workbook('new_sysfile.xlsx')

#Insert Worksheets
worksheet1 = workbook.add_worksheet('Sheet1')
worksheet2 = workbook.add_worksheet('Sheet2')
worksheet3 = workbook.add_worksheet('Sheet3')

cell_format = workbook.add_format()
cell_format.set_font_name('Arial')
cell_format.set_font_size('11')

worksheet1.set_landscape()
worksheet2.set_landscape()
worksheet3.set_landscape()

header1 = '&L&G' + '&R hText '

worksheet1.set_margins(top=1)
worksheet1.set_header(header1, {'image_left': logo})

worksheet2.set_margins(top=1)
worksheet2.set_header(header1, {'image_left': logo})

worksheet3.set_margins(top=1)
worksheet3.set_header(header1, {'image_left': logo})

workbook.close()
#############################################################


# opening the source excel file
sourcefile = "sysfile2.xlsx"
wb1 = xl.load_workbook(sourcefile)

ws1 = wb1["Sheet1"]
ws2 = wb1["Sheet2"]
ws3 = wb1["Sheet3"]

# opening the destination excel file
dest_file = "new_sysfile.xlsx"
wb2 = xl.load_workbook(dest_file)
ws_dest1 = wb2["Sheet1"]
ws_dest2 = wb2["Sheet2"]
ws_dest3 = wb2["Sheet3"]

# some formatting

# calculate total number of rows and

mr1 = ws1.max_row
mc1 = ws1.max_column

mr2 = ws2.max_row
mc2 = ws2.max_column

mr3 = ws3.max_row
mc3 = ws3.max_column

# copying the cell values from source

for i in range(1, mr1 + 1):
    for j in range(1, mc1 + 1):
        # reading cell value from source excel file
        c = ws1.cell(row=i, column=j)

        # writing the read value to destination excel file
        ws_dest1.cell(row=i, column=j).value = c.value

# SECOND SHEET
for i in range(1, mr2 + 1):
    for j in range(1, mc2 + 1):
        # reading cell value from source excel file
        c = ws2.cell(row=i, column=j)

        # writing the read value to destination excel file
        ws_dest2.cell(row=i, column=j).value = c.value

# THIRD SHEET
for i in range(1, mr3 + 1):
    for j in range(1, mc3 + 1):
        # reading cell value from source excel file
        c = ws3.cell(row=i, column=j)

        # writing the read value to destination excel file
        ws_dest3.cell(row=i, column=j).value = c.value

ws2.sheet_properties.pageSetUpPr.fitToPage = True
ws2.page_setup.fitToWidth = True

# (some formatting)

ws_dest1.sheet_properties.pageSetUpPr.fitToPage = True
ws_dest1.page_setup.fitToHeight = False

ws_dest2.sheet_properties.pageSetUpPr.fitToPage = True
ws_dest2.page_setup.fitToHeight = False

ws_dest3.sheet_properties.pageSetUpPr.fitToPage = True
ws_dest3.page_setup.fitToHeight = False

wb2.save(str(dest_file))

我希望有人有解决方案。谢谢你。

标签: pythonopenpyxlxlsxwriter

解决方案


推荐阅读