[英]How to compare column values of one excel file to the column values of another excel file in Python using openpyxl?
[英]Python (openpyxl) : Put data from one excel file to another (template file) & save it with another name while retaining the template
我有一個名為template.xlsx
的模板 excel文件,其中包含多個工作表。 我想將單獨的.csv
文件中的數據復制到第一張template.xlsx
(命名為data
)中,並將新文件保存為result.xlsx
同時保留原始模板文件。
我想從template.xlsx
的data
表中的第二行開始粘貼數據
這是我到目前為止開發的代碼
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)
我無法獲得所需的輸出
左側的模板文件(帶有一個額外的行)和右側的輸入文件(要復制到模板的數據),看起來像這樣
實際上並不需要使用shutil模塊,因為您可以使用openpyxl.load_workbook加載模板,然后使用其他名稱進行保存。
另外,你在for循環中的ws.append(r)
將附加到從template.xlsx獲取的現有數據,聽起來你只想保留標題。
我在下面提供了一個完全可重現的示例,它可以創建'template.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)
預期產出:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.