Search code examples
pythonexcelpandasopenpyxlworksheet

How to convert a dataframe to a worksheet (Python 3.8)?


for file in all_files:
    df = pd.read_excel(file,sheet_name='Application')
    new_df=df.iloc[2:4,]
   

After the lines of code above, I want to convert the dataframe "new_df" to a worksheet to un-merge columns of the excel sheet. What I tried was this: ws=openpyxl.utils.dataframe.dataframe_to_rows(new_df, index=True, header=True) according to https://openpyxl.readthedocs.io/en/stable/api/openpyxl.utils.dataframe.html but I get an error saying "Attribute error: module 'openpyxl.utils' has no attribute 'dataframe'. How do I fix this? Or is there another approach to un-merge columns?

All the imports I used:

import glob
import os
import pandas as pd
from pandas import DataFrame
import numpy
from bs4 import BeautifulSoup
import openpyxl
from openpyxl import utils
import requests
import urllib3
import selenium.webdriver as webdriver
from openpyxl.workbook import Workbook
from openpyxl import load_workbook
from openpyxl.utils.cell import range_boundaries```

Solution

  • Based on the openpyxl-pandas docs:

    from openpyxl.utils.dataframe import dataframe_to_rows