我能够打开已有的工作簿,但是我没有看到任何方法在该工作簿中打开预先存在的工作表.有没有办法做到这一点?
我目前有这个代码.它完美地运作.
它循环遍历文件夹中的excel文件,删除前两行,然后将它们保存为单独的excel文件,并将文件作为附加文件保存在循环中.
目前,每次运行代码时附加的文件都会覆盖现有文件.
我需要将新数据附加到已经存在的Excel工作表的底部('master_data.xlsx)
dfList = []
path = 'C:\\Test\\TestRawFile'
newpath = 'C:\\Path\\To\\New\\Folder'
for fn in os.listdir(path):
# Absolute file path
file = os.path.join(path, fn)
if os.path.isfile(file):
# Import the excel file and call it xlsx_file
xlsx_file = pd.ExcelFile(file)
# View the excel files sheet names
xlsx_file.sheet_names
# Load the xlsx files Data sheet as a dataframe
df = xlsx_file.parse('Sheet1',header= None)
df_NoHeader = df[2:]
data = df_NoHeader
# Save individual dataframe
data.to_excel(os.path.join(newpath, fn))
dfList.append(data)
appended_data = …
Run Code Online (Sandbox Code Playgroud) 您好,我已经看到了更复杂问题的解决方案,但我正在尝试执行以下操作:
将数据框附加到 Excel 表格。我有一个 Excel 文件,其中包含以下数据:
# Create Initial Excel
data = [['tom', 10,1,'a'], ['matt', 15,5,'b'],['nick', 18,2,'b'],['luke', 12,6,'b'],['geoff', 20,10,'a']]
# Create the pandas DataFrame
df = pd.DataFrame(data, columns = ['Name', 'Attempts','Score','Category'])
df
Name Attempts Score Category
0 tom 10 1 a
1 matt 15 5 b
2 nick 18 2 b
3 luke 12 6 b
4 geoff 20 10 a
df.to_excel('Excel.xlsx',index=False)
Run Code Online (Sandbox Code Playgroud)
每周我都会收到以下形式的新数据:
#New Dataframe
data2 = [['mick', 10,1,'a'], ['matt', 15,5,'b'],['jim', 18,2,'b'],['mark', 12,6,'b'],['geoff', 20,10,'a']]
df2 = pd.DataFrame(data2, columns = ['Name', …
Run Code Online (Sandbox Code Playgroud) 因任一错误而遇到麻烦;writer.book=book
AttributeError: can't set attribute 'book'
或者BadZipFile
对于没有给出 badzipfile 错误的代码,我首先放置了写入 excel 文件的代码行,dataOutput=pd.DataFrame(dictDataOutput,index=[0])
但是,即使我无法摆脱,writer.book = book AttributeError: can't set attribute 'book'
正如其中一个答案所示,我需要将 openpyxl 返回到以前的版本,或者使用 CSV 文件而不是 excel。我认为这不是解决方案。应该有我无法进入的解决方案
dataOutput=pd.DataFrame(dictDataOutput,index=[0])
dataOutput.to_excel('output.xlsx') 'output.xlsm'
book = load_workbook('output.xlsx') 'output.xlsm'
writer = pd.ExcelWriter('output.xlsx')OR'output.xlsm'#,engine='openpyxl',mode='a',if_sheet_exists='overlay')
writer.book = book
writer.sheets = {ws.title: ws for ws in book.worksheets}
for sheetname in writer.sheets:
dataOutput.to_excel(writer,sheet_name=sheetname, startrow=writer.sheets[sheetname].max_row, index = False,header= False)
writer.save()
Run Code Online (Sandbox Code Playgroud)
我在此处输入链接描述中寻找答案,并在此处输入链接描述中属性错误的详细解决方案中寻找答案
---我尝试了另一种方法
with pd.ExcelWriter('output.xlsx', mode='a',if_sheet_exists='overlay') as writer:
dataOutput.to_excel(writer, sheet_name='Sheet1')
writer.save()
Run Code Online (Sandbox Code Playgroud)
但是这次又报错了
FutureWarning: save is not part …
Run Code Online (Sandbox Code Playgroud) 我想将一些记录添加到excel文件中,并使用它pandas.ExcelWriter
来完成此操作(http://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.ExcelWriter.html?highlight=excelwriter#pandas.ExcelWriter
):
import pandas as pd
df = pd.DataFrame(data={'a':[4], 'b':['corn'], 'c':[0.5]})
with pd.ExcelWriter("test.xlsx", mode='a') as writer:
df.to_excel(writer)
Run Code Online (Sandbox Code Playgroud)
a,b,c是test.xlsx的标题名称
运行此程序,引发valueError:
ValueError Traceback (most recent call last)
<ipython-input-3-c643d22b4217> in <module>
----> 1 with pd.ExcelWriter("test.xlsx", mode='a') as writer:
2 df.to_excel(writer)
3
~/anaconda/lib/python3.6/site-packages/pandas/io/excel.py in __init__(self, path, engine, date_format, datetime_format, mode, **engine_kwargs)
1935
1936 if mode == 'a':
-> 1937 raise ValueError('Append mode is not supported with xlsxwriter!')
1938
1939 super(_XlsxWriter, self).__init__(path, engine=engine,
ValueError: Append mode is not supported with xlsxwriter!
Run Code Online (Sandbox Code Playgroud)
不知道为什么
pandas ×4
python ×4
excel ×2
append ×1
for-loop ×1
openpyxl ×1
python-3.x ×1
worksheet ×1
xlsxwriter ×1