创建表时列标题消失

问题描述 投票:0回答:1

我有以下脚本,该脚本需要一个xls文件并创建一个带有表(格式为实际表)的xlsx。列标题在最终表中显示为Column1 Column2等,而不是在数据框中找到的实际标题。

是否有解决此问题的方法?我还发现自己创建了一个临时xlsx文件,将其复制以包含该表,然后删除该临时版本。是否可以进一步简化?

代码:

import os
import shutil
import pandas as pd
import xlsxwriter
import xlrd
from datetime import datetime 

date = datetime.today().strftime('%Y-%m-%d')

demand = r"C:\Users\xxxx\Desktop\source.xls"
dfd = pd.read_excel(demand, sheet_name = 'sheet').fillna(0)
dfd = dfd.iloc[6:]
dfd.columns = dfd.iloc[0] #replace headers
dfd = dfd[1:] #remove headers from first row

destd = r"C:\Users\xxxx\Desktop\temporary.xlsx"
destd2 = r"C:\Users\xxxx\Desktop\File (" + str(date) + ").xlsx"
dfd.to_excel(destd)

workbook = xlsxwriter.Workbook(destd)
worksheet = workbook.add_worksheet("Demand")
worksheet.add_table('A1:DL10000', {'data': dfd.values.tolist()})
workbook.close()
destination = shutil.copy2(destd, destd2)

os.remove(destd)
python pandas xlsxwriter
1个回答
0
投票

从xlsxwriter的文档here

The columns parameter can be used to set properties for columns within the table.
The sub-properties that can be set are header, header_format etc.
The column data must be specified as a list of dicts.

这意味着您可以在add_table方法的字典中添加另一个键'columnms'以重命名标题。

这里是表头重命名的示例:

import pandas as pd

df = pd.DataFrame({'Name': ['John','George','Paul'],
                    'Age': [23,34,42]})

writer = pd.ExcelWriter('test.xlsx', engine='xlsxwriter')
df.to_excel(writer, sheet_name='Sheet1', index=False)
workbook  = writer.book
worksheet = writer.sheets['Sheet1']

# This will create a basic table with the headers named as 'Column 1' etc
#worksheet.add_table('A1:B4', {'data': df.values.tolist()})

# Add the 'columns' argument to name the headers
worksheet.add_table('A1:B4', {'data': df.values.tolist(),
                    'columns': [{'header': 'Name'},
                                {'header': 'Age'}]})

writer.save()

输出:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.