Я пытался взять лист из электронной таблицы xls и добавить его на лист в другой электронной таблице, содержащей несколько листов, без перезаписи всего файла и удаления других листов внутри него.
Для этогоЯ импортировал первый лист как фрейм данных pandas, а затем пропустил его через функцию, однако он всегда показывает ошибку: объект «список» не имеет атрибута «find»
Вот код, который я использовалдля этого:
import pandas as pd
def append_df_to_excel(filename, df, sheet_name='Sheet1', startrow=None,
truncate_sheet=False,
**to_excel_kwargs):
"""
Append a DataFrame [df] to existing Excel file [filename]
into [sheet_name] Sheet.
If [filename] doesn't exist, then this function will create it.
Parameters:
filename : File path or existing ExcelWriter
(Example: '/path/to/file.xlsx')
df : dataframe to save to workbook
sheet_name : Name of sheet which will contain DataFrame.
(default: 'Sheet1')
startrow : upper left cell row to dump data frame.
Per default (startrow=None) calculate the last row
in the existing DF and write to the next row...
truncate_sheet : truncate (remove and recreate) [sheet_name]
before writing DataFrame to Excel file
to_excel_kwargs : arguments which will be passed to `DataFrame.to_excel()`
[can be dictionary]
Returns: None
"""
from openpyxl import load_workbook
# ignore [engine] parameter if it was passed
if 'engine' in to_excel_kwargs:
to_excel_kwargs.pop('engine')
writer = pd.ExcelWriter(filename, engine='openpyxl')
# Python 2.x: define [FileNotFoundError] exception if it doesn't exist
try:
FileNotFoundError
except NameError:
FileNotFoundError = IOError
try:
# try to open an existing workbook
writer.book = load_workbook(filename)
# get the last row in the existing Excel sheet
# if it was not specified explicitly
if startrow is None and sheet_name in writer.book.sheetnames:
startrow = writer.book[sheet_name].max_row
# truncate sheet
if truncate_sheet and sheet_name in writer.book.sheetnames:
# index of [sheet_name] sheet
idx = writer.book.sheetnames.index(sheet_name)
# remove [sheet_name]
writer.book.remove(writer.book.worksheets[idx])
# create an empty sheet [sheet_name] using old index
writer.book.create_sheet(sheet_name, idx)
# copy existing sheets
writer.sheets = {ws.title:ws for ws in writer.book.worksheets}
except FileNotFoundError:
# file does not exist yet, we will create it
pass
if startrow is None:
startrow = 0
# write out the new sheet
df.to_excel(writer, sheet_name, startrow=startrow, **to_excel_kwargs)
# save the workbook
writer.save()
dataFrame = pd.read_excel(r'C:\John\spreadsheets\royalty_test\Rawfiles\Blackwells_royalty_Other_2018_04.xls')
append_df_to_excel(r'C:\John\spreadsheets\royalty_test\Blackwells_NON-OS_Royalty_2018_04.xlsx',dataFrame,sheet_name='Orders',index=False)
Трассировка выглядит следующим образом:
runfile('C:/John/python/practicepython/sdafdf.py', wdir='C:/John/python /practicepython')
Traceback (most recent call last):
File "<ipython-input-255-58955ae481da>", line 1, in <module>
runfile('C:/John/python/practicepython/sdafdf.py', wdir='C:/John/python/practicepython')
File "C:\Users\John\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 705, in runfile
execfile(filename, namespace)
File "C:\Users\John\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "C:/John/python/practicepython/sdafdf.py", line 86, in <module>
append_df_to_excel(r'C:\John\spreadsheets\royalty_test\Blackwells_NON-OS_Royalty_2018_04.xlsx',dataFrame,sheet_name='Orders',index=False)
File "C:/John/python/practicepython/sdafdf.py", line 50, in append_df_to_excel
writer.book = load_workbook(filename)
File "C:\Users\John\Anaconda3\lib\site-packages\openpyxl\reader\excel.py", line 224, in load_workbook
pivot_caches = parser.pivot_caches
File "C:\Users\John\Anaconda3\lib\site-packages\openpyxl\packaging\workbook.py", line 126, in pivot_caches
records = get_rel(self.archive, cache.deps, cache.id, RecordList)
File "C:\Users\John\Anaconda3\lib\site-packages\openpyxl\packaging\relationship.py", line 153, in get_rel
rel = next(deps.find(cls.rel_type))