且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用openpyxl将pandas数据框复制到excel

更新时间:2023-01-30 22:01:14

openpyxl 2.4附带了一个实用程序,可将Pandas Dataframe转换为openpyxl可直接使用的格式.代码看起来像这样:

openpyxl 2.4 comes with a utility for converting Pandas Dataframes into something that openpyxl can work with directly. Code would look a bit like this:

from openpyxl.utils.dataframe import dataframe_to_rows
rows = dataframe_to_rows(df)

for r_idx, row in enumerate(rows, 1):
    for c_idx, value in enumerate(row, 1):
         ws.cell(row=r_idx, column=c_idx, value=value)

您可以调整枚举的开始位置,以将单元格放置在所需的位置.

You can adjust the start of the enumeration to place the cells where you need them.

有关更多信息,请参见 openpyxl文档.

See openpyxl documentation for more information.