簡體   English   中英

Pandas 數據框到 excel 文件中的特定工作表而不會丟失格式

[英]Pandas dataframe to specific sheet in a excel file without losing formatting

我有一個如下所示的數據框

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=',')

我想將此數據框寫入文件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 文件中的特定工作表? 不丟失目標文件的格式

您只需使用to_excel 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()

您是否僅限於使用 pandas 或 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

讓我知道這是否達到了您的要求。

您的問題的解決方案在這里: 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()

我花了一整天的時間(我的一個同事花了更長的時間)。 值得慶幸的是,它似乎對我有用 - 將 dataframe 粘貼到 Excel 工作表中,而不更改任何 Excel 源格式。 它需要pywin32 package,它像用戶一樣“驅動” Excel,使用 VBA。

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中做你想做的事情,然后讀取生成的宏代碼是有幫助的。 通常這會給你很好的線索,讓你知道你可以調用什么命令。

你可以試試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')  

請參閱示例

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM