'how to parse Pandas Dataframe into the specific sheet of macro file
I would like to parse 2 columns in dataframe sample below into the excel macro workbook (xlsm),
import pandas as pd
from openpyxl.utils.dataframe import dataframe_to_rows
from openpyxl import *
excel_path = "test.xlsm"
df.head()
HT D Group
0 A BL
1 B 7 Group D
2 C NaN
3 D 5 Group C
4 E 2 group A
5 F NaN
6 G 3 Group B
df_temp = df[['HT','D']]
I would like to parse df_temp into column A and B on Sheet1 of the macro file (path_excel). I tried with the code below:
wb = load_workbook(path_excel,read_only=False, keep_vba=True)
ws = wb.get_sheet_by_name('Sheet1')
ws['A1']
for r in dataframe_to_rows(df_temp,index=False,header=False):
print(r)
(ws.max_row + 1).append(r)
but it could not write the data from the df_temp to the macro file as I expected , Could you please help look it ?
Solution 1:[1]
I have to solve my issue already , it just simple edit
import pandas as pd
from openpyxl.utils.dataframe import dataframe_to_rows
from openpyxl import *
excel_path = "test.xlsm"
df.head()
HT D Group
0 A BL
1 B 7 Group D
2 C NaN
3 D 5 Group C
4 E 2 group A
5 F NaN
6 G 3 Group B
df_temp = df[['HT','D']]
wb = load_workbook(path_excel,read_only=False, keep_vba=True)
ws = wb['Sheet1']
for r in dataframe_to_rows(df_temp,index=False,header=False):
ws.append(r)
wb.save(excel_path)
That's all. in the excel file, open_workbook event has been configured already. just use python to start this file then it could work as fine.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | van thang |
