合并两个.xlsx文件,包括格式

时间:2020-11-10 08:49:10

标签: python openpyxl xlsxwriter

我想合并两个.xlsx文件。我在文件header_footer.xlsx中用openpyxl编写了页眉和页脚内容。在第二个文件sys.xlsx中,我有很多带有内容和格式(字体,背景色,单元格宽度等)的单元格。我想合并包含header_footer.xlsx文件中格式(!)的内容。我不知道该怎么做。希望有一些提示。

这是我目前的代码:

import openpyxl as xl
import xlsxwriter

# define the sourcefile
sourcefile = "sys.xlsx"
wb1 = xl.load_workbook(sourcefile)
ws1 = wb1.worksheets[0]

# opening destination file
dest_file = "header_footer.xlsx"
wb2 = xl.load_workbook(dest_file)
ws2 = wb2.active

# count rows and columns
mr = ws1.max_row
mc = ws1.max_column

# copy and insert content
for i in range(1, mr + 1):
    for j in range(1, mc + 1):
        c = ws1.cell(row=i, column=j)

        ws2.cell(row=i, column=j).value = c.value

    # save the file
wb2.save(str(dest_file))

谢谢。 PH_0x17

0 个答案:

没有答案