【问题标题】:Overwrite sheets in Excel with Python用 Python 覆盖 Excel 中的工作表
【发布时间】:2019-07-24 00:38:32
【问题描述】:

我是 Python 新手(和一般编程),在将数据写入 Excel 中的工作表时遇到了问题。

我正在读取 Excel 文件,对特定列执行总和计算,然后将结果写入新工作簿。最后,它会根据结果创建两个图表。

代码有效,除了每次我运行它时,它都会创建新的工作表,并在末尾附加数字。我真的只是希望它覆盖我提供的工作表名称,而不是创建新的。

我对所有模块不够熟悉,无法理解所有可用选项。我研究了 openpyxl 和 pandas,以及我正在尝试做的类似示例要么不容易找到,要么在我尝试时似乎不起作用。

import pandas as pd
import xlrd
import openpyxl as op
from openpyxl import load_workbook
import matplotlib.pyplot as plt

# declare the input file
input_file = 'TestData.xlsx'

# declare the output_file name to be written to
output_file = 'TestData_Output.xlsx'
book = load_workbook(output_file)
writer = pd.ExcelWriter(output_file, engine='openpyxl')
writer.book = book

# read the source Excel file and calculate sums
excel_file = pd.read_excel(input_file)
num_events_main = excel_file.groupby(['Column1']).sum()
num_events_type = excel_file.groupby(['Column2']).sum()

# create dataframes and write names and sums out to new workbook/sheets
df_1 = pd.DataFrame(num_events_main)
df_2 = pd.DataFrame(num_events_type)
df_1.to_excel(writer, sheet_name = 'TestSheet1')
df_2.to_excel(writer, sheet_name = 'TestSheet2')

# save and close
writer.save()
writer.close()

# dataframe for the first sheet
df = pd.read_excel(output_file, sheet_name='TestSheet1')
values = df[['Column1', 'Column3']]

# dataframe for the second sheet
df = pd.read_excel(output_file, sheet_name='TestSheet2')
values_2 = df[['Column2', 'Column3']]

# create the graphs
events_graph = values.plot.bar(x = 'Column1', y = 'Column3', rot = 60) # rot = rotation
type_graph = values_2.plot.bar(x = 'Column2', y = 'Column3', rot = 60) # rot = rotation
plt.show()

我得到了预期的结果,并且图表工作正常。我真的很想在每次运行时覆盖工作表。

【问题讨论】:

  • 不使用load_workbook怎么办?据我回忆,pd.ExcelWriter 在初始化时会覆盖现有工作簿。
  • writer = pd.ExcelWriter(output_file, engine='openpyxl')已经有一本书了,所以没有理由使用book = load_workbook(output_file)
  • dubbbdan,谢谢!那行得通!感谢您的帮助。
  • 在您的帖子中发布了答案。请接受并投票(如果您认为值得)。

标签: python


【解决方案1】:

来自pd.DataFrame.to_excel 文档:

可以通过指定唯一的 sheet_name 来写入多个工作表。 将所有数据写入文件后,有必要保存更改。 请注意,创建一个 ExcelWriter 对象的文件名已经 存在将导致现有文件的内容被删除。

试着像写书一样

import pandas as pd
df = pd.DataFrame({'col1':[1,2,3],'col2':[4,5,6]})
writer = pd.ExcelWriter('g.xlsx')
df.to_excel(writer, sheet_name = 'first_df')
df.to_excel(writer, sheet_name = 'second_df')
writer.save()

如果您检查工作簿,您将有两个工作表。

然后假设您想将新数据写入同一个工作簿:

writer = pd.ExcelWriter('g.xlsx')
df.to_excel(writer, sheet_name = 'new_df')
writer.save()

如果您现在检查工作簿,您将只有一个名为 new_df 的工作表

如果您要保留 Excel 文件中的其他工作表并仅覆盖所需的工作表,则需要使用 load_workbook

在写入任何数据之前,您可以删除要写入的工作表:

std=book.get_sheet_by_name(<sheee_name>)
book.remove_sheet(std)

一旦您尝试编写具有重复工作表名称的工作簿,这将停止将数字附加到工作表名称的行为。

【讨论】:

    猜你喜欢
    • 2016-06-14
    • 2021-12-26
    • 2019-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多