将数据框Pandas到excel文件中的特定工作表,而不会丢失格式

4xrmg8kj  于 2022-12-21  发布在  其他
关注(0)|答案(5)|浏览(163)

我有一个如下所示的 Dataframe

Date,cust,region,Abr,Number,         
12/01/2010,Company_Name,Somecity,Chi,36,
12/02/2010,Company_Name,Someothercity,Nyc,156,

df = pd.read_clipboard(sep=',')

我希望将此 Dataframe 写入文件output.xlsx中的特定工作表(称为temp_data
所以我试了下面的

import pandas
from openpyxl import load_workbook

book = load_workbook('output.xlsx')
writer = pandas.ExcelWriter('output.xlsx', engine='openpyxl') 
writer.book = book
writer.sheets = dict((ws.title, ws) for ws in book.worksheets)

我也试过下面的

path = 'output.xlsx'

with pd.ExcelWriter(path) as writer:
    writer.book = openpyxl.load_workbook(path)
    final_df.to_excel(writer, sheet_name='temp_data',startrow=10)
writer.save()

但我不确定我是否过于复杂。我得到一个错误,如下图所示。但我在任务管理器中验证,没有excel文件/任务正在运行
BadZipFile:文件不是zip文件
此外,我也失去了我的格式的output.xlsx文件时,我设法写的文件基于以下建议。我已经有一个整齐的格式化字体,颜色文件等,只需要把数据里面。

有没有办法将Pandas数据框写入现有excel文件中的特定工作表?而不会丢失目的地文件的格式

dw1jzc5e

dw1jzc5e1#

您只需使用panda Dataframe 中的to_excel
尝试以下片段:

df1.to_excel("output.xlsx",sheet_name='Sheet_name')

如果存在现有数据,请尝试以下片段:

writer = pd.ExcelWriter('output.xlsx', engine='openpyxl')
# try to open an existing workbook
writer.book = load_workbook('output.xlsx')
df.to_excel(writer,index=False,header=False,startrow=len(reader)+1)
writer.save()
writer.close()
roqulrg3

roqulrg32#

您是否仅限于使用panda或openpyxl?因为如果您习惯使用其他库,最简单的方法可能是使用win32com来傀儡excel,就好像您是一个手动复制和粘贴信息的用户一样。

import pandas as pd
import io
import win32com.client as win32
import os

csv_text = """Date,cust,region,Abr,Number      
12/01/2010,Company_Name,Somecity,Chi,36
12/02/2010,Company_Name,Someothercity,Nyc,156"""


df = pd.read_csv(io.StringIO(csv_text),sep = ',')
temp_path = r"C:\Users\[User]\Desktop\temp.xlsx" #temporary location where to write this dataframe
df.to_excel(temp_path,index = False) #temporarily write this file to excel, change the output path as needed

excel = win32.Dispatch("Excel.Application")
excel.Visible = True #Switch these attributes to False if you'd prefer Excel to be invisible while excecuting this script
excel.ScreenUpdating = True 

temp_wb = excel.Workbooks.Open(temp_path)
temp_ws = temp_wb.Sheets("Sheet1")

output_path = r"C:\Users\[User]\Desktop\output.xlsx" #Path to your output excel file
output_wb = excel.Workbooks.Open(output_path)
output_ws = output_wb.Sheets("Output_sheet")

temp_ws.Range('A1').CurrentRegion.Copy(Destination = output_ws.Range('A1')) # Feel free to modify the Cell where you'd like the data to be copied to
input('Check that output looks like you expected\n') # Added pause here to make sure script doesn't overwrite your file before you've looked at the output

temp_wb.Close()
output_wb.Close(True) #Close output workbook and save changes
excel.Quit() #Close excel
os.remove(temp_path) #Delete temporary excel file

如果这能达到你的目的请告诉我。

p3rjfoxz

p3rjfoxz3#

我花了一整天的时间在这上面(我的一个同事花了更长的时间)。谢天谢地,这似乎符合我的目的--将 Dataframe 粘贴到Excel工作表中,而不更改任何Excel源格式。它需要pywin32包,该包使用VBA“驱动”Excel,就像它是用户一样。

import pandas as pd
from win32com import client

# Grab your source data any way you please - I'm defining it manually here:
df = pd.DataFrame([
['LOOK','','','','','','','',''],
['','MA!','','','','','','',''],
['','','I pasted','','','','','',''],
['','','','into','','','','',''],
['','','','','Excel','','','',''],
['','','','','','without','','',''],
['','','','','','','breaking','',''],
['','','','','','','','all the',''],
['','','','','','','','','FORMATTING!']
])

# Copy the df to clipboard, so we can later paste it as text.
df.to_clipboard(index=False, header=False) 

excel_app = client.gencache.EnsureDispatch("Excel.Application") # Initialize instance

wb = excel_app.Workbooks.Open("Template.xlsx") # Load your (formatted) template workbook
ws = wb.Worksheets(1) # First worksheet becomes active - you could also refer to a sheet by name
ws.Range("A3").Select() # Only select a single cell using Excel nomenclature, otherwise this breaks
ws.PasteSpecial(Format='Unicode Text') # Paste as text
wb.SaveAs("Updated Template.xlsx") # Save our work
excel_app.Quit() # End the Excel instance

一般来说,当使用win32com方法时,记录自己(使用宏)在Excel中执行的操作,然后阅读生成的宏代码是很有帮助的。通常,这会为您提供关于可以调用哪些命令的极好线索。

0s7z1bwu

0s7z1bwu4#

您的问题的解决方案存在于此处:How to save a new sheet in an existing excel file, using Pandas?
要从df添加新页面:

import pandas as pd
from openpyxl import load_workbook
import os
import numpy as np

os.chdir(r'C:\workdir')

path = 'output.xlsx'
book = load_workbook(path)
writer = pd.ExcelWriter(path, engine = 'openpyxl')
writer.book = book
### replace with your df ###
x = np.random.randn(100, 2)
df = pd.DataFrame(x)

df.to_excel(writer, sheet_name = 'x')
writer.save()
writer.close()
xbp102n0

xbp102n05#

您可以尝试xltpl
基于output.xlsx文件创建样板文件。
使用您的数据渲染文件。

from xltpl.writerx import BookWriterx  
writer = BookWriterx('template.xlsx')  
d = {'rows': df.values}
d['tpl_name'] = 'tpl_sheet'  
d['sheet_name'] = 'temp_data'  
writer.render_sheet(d)  
d['tpl_name'] = 'other_sheet'  
d['sheet_name'] = 'other'  
writer.render_sheet(d)  
writer.save('out.xls')

参见examples

相关问题