简体   繁体   English

Python(openpyxl):将数据从一个excel文件放到另一个excel文件中(模板文件)并使用其他名称保存,同时保留模板

[英]Python (openpyxl) : Put data from one excel file to another (template file) & save it with another name while retaining the template

I have a template excel file named as template.xlsx which has a number of sheets. 我有一个名为template.xlsx模板 excel文件,其中包含多个工作表。 I would like to copy data from a seperate .csv file into the first sheet of template.xlsx (named as data ) and save the new file as result.xlsx while retaining the original template file. 我想将单独的.csv文件中的数据复制到第一张template.xlsx (命名为data )中,并将新文件保存为result.xlsx同时保留原始模板文件。

I want to paste the data starting from the second row in the data sheet of template.xlsx 我想从template.xlsxdata表中的第二行开始粘贴数据

This is the code I have developed so far 这是我到目前为止开发的代码

import pandas as pd
from openpyxl.utils.dataframe import dataframe_to_rows
import openpyxl
from shutil import copyfile

template_file = 'template.xlsx' # Has a header in row 1 already which needs to be skipped while pasting data but it should be there in the output file
output_file = 'result.xlsx' 

copyfile(template_file, output_file)
df = pd.read_csv('input_file.csv') #The file which is to be pasted in the template

wb = openpyxl.load_workbook(output_file)
ws = wb.get_sheet_by_name('data') #Getting the sheet named as 'data'

for r in dataframe_to_rows(df, index=False, header=False):
   ws.append(r)

 wb.save(output_file)

I am not able to get the desired output 我无法获得所需的输出

The template file (with an extra row) on the left and the input file (data to be copied to the template) on the right, look like this 左侧的模板文件(带有一个额外的行)和右侧的输入文件(要复制到模板的数据),看起来像这样

模板 在此输入图像描述

There's not really any need to use the shutil module, as you could just use openpyxl.load_workbook to load your template and then save by a different name. 实际上并不需要使用shutil模块,因为您可以使用openpyxl.load_workbook加载模板,然后使用其他名称进行保存。

Additionally, ws.append(r) inside you're for loop will append to the existing data taken form template.xlsx, and it sounds like you only want to keep the header. 另外,你在for循环中的ws.append(r)将附加到从template.xlsx获取的现有数据,听起来你只想保留标题。

I've provided a fully reproducible example below that creates 'template.xlsx' for demonstration purposes. 我在下面提供了一个完全可重现的示例,它可以创建'template.xlsx'用于演示目的。 Then it loads 'template.xlsx' adds new data to it and saves as result.xlsx. 然后加载'template.xlsx'向其添加新数据并保存为result.xlsx。

from openpyxl import Workbook
from openpyxl import load_workbook
from openpyxl.utils.dataframe import dataframe_to_rows
from openpyxl.chart import PieChart, Reference, Series
import pandas as pd

template_file = 'template.xlsx'
output_file = 'result.xlsx'

#This part creates a workbook called template.xlsx with a sheet called 'data' and sheet called 'second_sheet'
writer = pd.ExcelWriter('template.xlsx', engine='openpyxl') 
wb  = writer.book
df = pd.DataFrame({'Pie': ["Cream", "Cherry", "Banoffee", "Apple"],
                  'Sold': [2, 2, 1, 4]})

df.to_excel(writer, index=False, sheet_name='data', startrow=1)
ws = writer.sheets['data']
ws['A1'] = 1
ws['B1'] = 2

ch = PieChart()
labels = Reference(ws, min_col=1, min_row=3, max_row=6)
data = Reference(ws, min_col=2, min_row=3, max_row=6)
ch.series = (Series(data),)
ch.title = "Pies sold"
ws.add_chart(ch, "D2")

ws = wb.create_sheet("Second_sheet")
ws['A1'] = 'This Sheet will not be overwitten'

wb.save(template_file)

#Now we load workbook called template.xlsx modify the 'data' sheet and save under a new name
#template.xlsx has not been modified

df_new = pd.DataFrame({'different_name': ["Blueberry", "Pumpkin", "Mushroom", "Turnip"],
                  'different_numbers': [4, 6, 2, 1]})

wb = load_workbook(template_file)

ws = wb.get_sheet_by_name('data') #Getting the sheet named as 'data'

rows = dataframe_to_rows(df_new, index=False, header=False)

for r_idx, row in enumerate(rows, 1):
    for c_idx, value in enumerate(row, 1):
         ws.cell(row=r_idx+2, column=c_idx, value=value)

wb.save(output_file)

Expected Output: 预期产出:

两本工作簿的预期输出

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 openpyxl 将一个 excel 文件的列值与 Python 中另一个 excel 文件的列值进行比较? - How to compare column values of one excel file to the column values of another excel file in Python using openpyxl? 使用python将特定数据从一个Excel文件传输到另一个 - Transferring specific data from one excel file to another using python 是否可以从一个文件到另一个文件使用 Django 模板块? - Is it possible to use Django template block from one file to another file? 如何在保留格式的同时使用 python 将 excel 文件翻译成另一种语言 - How to translate a excel file to another language using python while retaining the format 使用 python 中的 openpyxl 将特定行和列添加到另一个 excel 文件中 - add specific rows and column into another excel file using openpyxl in python Openpyxl:代码无法将数据从一个 Excel 电子表格写入另一个 - Openpyxl: Code fails to write data from one Excel spreadsheet to another python 使用模板将数据从一个字典迁移到另一个字典 - python migrate data from one dict into another dict using a template 如何在跳过空单元格的同时将数据从一张纸复制到另一张纸 - Python 和 Openpyxl - How to copy data from one sheet to another while skipping empty cells - Python and Openpyxl 使用Python从一个文件夹中读取excel文件并输出到另一个文件夹中的csv文件时收到PermissionError? - Receiving PermissionError while reading excel file from one folder and outputting to csv file in another folder using Python? 如何将一个 python 文件中的全局变量显示到另一个文件中(作为 Jinja 模板) - How do I display a global variable from one python file into another(as a Jinja Template)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM