,我一回到透視表的出口專題大熊貓dataframes我找到了出口較好的庫。 Openpyxl!隨着openpyxl它可以打開預定義的Excel模板這樣它沒有必要處理不必要的xlsxwriter錯誤寫預定好看tablehead下方的數據幀的數據。這裏是openpyxl中的示例代碼:
import openpyxl
from openpyxl import load_workbook
workbook.active = 0
worksheet = workbook.active
worksheet.title = 'XYZ'
#check length of df
depth_df_2 = len(merged_plz_all)
#call special method to comfortably write the dataframe below your
#predefined header
update_range(workbook.active,merged_plz_all,cell_range =
'A18:'+str(spaltenindex[len(merged_plz_all.columns)])+str(depth_df_2+17))
workbook.save('yourNicelyLookingPivotTable.xlsx')
這裏是我在另一個stackoverflow線程中找到的必需的update_range方法。我不幸didnt書籤,所以我請求原諒不提供update_range方法的起源。我個人認爲這種方法應該是圖書館openpyxl本身的一部分!
def update_range(worksheet, data, cell_range=None, named_range=None):
"""
Updates an excel worksheet with the given data.
:param worksheet: an excel worksheet
:param data: data used to update the worksheet cell range (list, tuple, np.ndarray, pd.Dataframe)
:param cell_range: a string representing the cell range, e.g. 'AB12:XX23'
:param named_range: a string representing an excel named range
"""
def clean_data(data):
if not isinstance(data, (list, tuple, np.ndarray, pd.DataFrame)):
raise TypeError('Invalid data, data should be an array type iterable.')
if not len(data):
raise ValueError('You need to provide data to update the cells')
if isinstance(data, pd.DataFrame):
data = data.values
elif isinstance(data, (list, tuple)):
data = np.array(data)
return np.hstack(data)
def clean_cells(worksheet, cell_range, named_range):
# check that we can access a cell range
if not any((cell_range, named_range) or all((cell_range, named_range))):
raise ValueError('`cell_range` or `named_range` should be provided.')
# get the cell range
if cell_range:
try:
cells = np.hstack(worksheet[cell_range])
except (CellCoordinatesException, AttributeError):
raise ValueError('The cell range provided is invalid, cell range must be in the form XX--[:YY--]')
else:
try:
cells = worksheet.get_named_range(named_range)
except (TypeError):
raise ValueError('The current worksheet {} does not contain any named range {}.'.format(
worksheet.title,
named_range))
# checking that we have cells to update, and data
if not len(cells):
raise ValueError('You need to provide cells to update.')
return cells
cells = clean_cells(worksheet, cell_range, named_range)
data = clean_data(data)
# check that the data has the same dimension as cells
if len(cells) != data.size:
raise ValueError('Cells({}) should have the same dimension as the data({}).'.format(len(cells), data.size))
for i, cell in enumerate(cells):
cell.value = data[i]
'T-Class'是'columns'對象的名稱。 'to_excel'不打擾寫出來'columns'對象的名稱。如果你想要這個確切的格式,你需要做一些自定義的寫作。完全可行,但不容易通過'to_excel'選項實現。 – piRSquared
你可以給出示例代碼或提示如何做到這一確切格式? – MBUser