python - 合并两个 .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))
我希望有人有解决方案。谢谢你。
解决方案
推荐阅读
- android - 如何在 android studio 中找到 SHA-1 密钥(用于 firebase)
- java - Java String 句子 - 第一个字母大写,第二个字母大写,依此类推
- python - 如何导入其他python模块和包
- oracle - Oracle Apex 锁定我的应用程序 - 无法访问我自己的应用程序
- html - 停留在背景之上的化身永远不起作用?
- eclipse-plugin - 项目资源管理器删除操作未按预期工作
- socket.io - 有没有办法用socket.io传递媒体文件?
- odoo - 从现有的 Odoo 数据库创建新的 Odoo 实例
- .net-5 - 如何在 Blazor wasm 中对字符串进行哈希处理?
- scala - Scala 3 库是否有一个适用于 `derives` 的 Show 类型类?