XLSXWriter - 格式化列表中的列

XLSWriter - format columns from list

我正在从数据帧创建 excel。 excel 的输出有 1300 列。某些列需要是 excel 中的日期格式(数据框中已经是日期时间格式)。这些列在 header.

中有“时间”一词

3 列数据的样本:

Dyn_10     Dyn_Price_10     Dyn_Time_10
-2             0.9493       44026.90764
-5             0.9625       44034.65486
0              0.9535       44035.67569
-10            0.9535       44035.67569

我现有的代码找出其中带有时间的列号,然后遍历 TimesColsNum 列表中的列并设置格式::

TimeCols = np.core.defchararray.find(CombinedDFDict[s].columns.values.astype(str), 'Time') >= 0
TimeColsNum = [i for i, x in enumerate(TimeCols) if x]
for t in TimeColsNum:
     wss.set_column(t, 20, date_format)
wb.close()

问题是我收到一个错误。我去掉这两行代码,没有报错。

for t in TimeColsNum:
     wss.set_column(t, 20, date_format)

我不知道该怎么办。我感谢所有指导。

这是回溯:

Traceback (most recent call last):

  File "C:\Users\Path\DynamicExitComparisonsv6.py", line 815, in <module>
    wb.close()

  File "C:\ProgramData\Anaconda3\lib\site-packages\xlsxwriter\workbook.py", line 320, in close
    self._store_workbook()

  File "C:\ProgramData\Anaconda3\lib\site-packages\xlsxwriter\workbook.py", line 685, in _store_workbook
    xml_files = packager._create_package()

  File "C:\ProgramData\Anaconda3\lib\site-packages\xlsxwriter\packager.py", line 135, in _create_package
    self._write_worksheet_files()

  File "C:\ProgramData\Anaconda3\lib\site-packages\xlsxwriter\packager.py", line 190, in _write_worksheet_files
    worksheet._assemble_xml_file()

  File "C:\ProgramData\Anaconda3\lib\site-packages\xlsxwriter\worksheet.py", line 3871, in _assemble_xml_file
    self._write_cols()

  File "C:\ProgramData\Anaconda3\lib\site-packages\xlsxwriter\worksheet.py", line 5416, in _write_cols
    self._write_col_info(self.colinfo[col])

  File "C:\ProgramData\Anaconda3\lib\site-packages\xlsxwriter\worksheet.py", line 5445, in _write_col_info
    if width > 0:

TypeError: '>' not supported between instances of 'Format' and 'int'

XlsxWriter worksheet.set_column() 的语法是:

set_column(first_col, last_col, width, cell_format, options)

在您的示例中,您只设置了其中一列。试试这个:

wss.set_column(t, t, 20, date_format)