Python Forum
Problem in saving .xlsm (excel) file using pandas dataframe in python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problem in saving .xlsm (excel) file using pandas dataframe in python
#1
I have two macro excel files(.xlsm file) with different information. I have written the python code that basically checks certain fields, and if that certain field is present then it saves in one folder, otherwise if that field is absent then it saves in another folder. I don't want any information removed from that excel file. I just wanted if that field is present then save the original file into that folder, otherwise save the original file in other folder. Code is not giving any error. But when I check the saved file, it is showing this error. Image Attached. Any help???

For testing, Input Files(.xlsm) are attached here

from pathlib import Path
import time
import parser
import argparse
import pandas as pd
import os
import warnings

warnings.filterwarnings("ignore")

parser = argparse.ArgumentParser(description="Process some integers.")

parser.add_argument("path", help="define the directory to folder/file")
parser.add_argument("--verbose", help="display processing information")

start = time.time()


def main(path_xlsm, verbose):
    if (".xlsm" in str(path_xlsm).lower()) and path_xlsm.is_file():
        xlsm_files = [Path(path_xlsm)]
    else:
        xlsm_files = list(Path(path_xlsm).glob("*.xlsm"))

    df = pd.DataFrame()
    
    for fn in xlsm_files:
        all_dfs = pd.read_excel(fn, sheet_name=None, header=None, engine="openpyxl")
        print(all_dfs)
        list_data = all_dfs.keys()
        all_dfs.pop("Lookups", None)
        all_dfs.pop("Instructions For Use", None)
        all_dfs.pop("Drop Down Boxes", None)
        all_dfs.pop("ResolutionLookups", None)
        
        for ws in list_data:  # Looping for excel sheet
            df1 = all_dfs[ws]
              
            if df1.iloc[3, 0] == "Client Representative" and df1.iloc[4, 1] == "DATE" and df1.iloc[4, 3] == "SHIFT":
                path_save = "C:\\Users\\ShantanuGupta\\Desktop\\Incoming\\Peel"
                df.to_excel(os.path.join(path_save, f"{fn.name}"), index=False)
            else:
                path_save = "C:\\Users\\ShantanuGupta\\Desktop\\Incoming\\Resolution"
                df.to_excel(os.path.join(path_save, f"{fn.name}"), index=False)
            
            
if __name__ == "__main__":
    start = time.time()
    args = parser.parse_args()
    path = Path(args.path)
    verbose = args.verbose
    main(path, verbose)  # Calling Main Function
    print("Processed time:", time.time() - start)  # Total Time 

Attached Files

.py   Incoming.py (Size: 1.84 KB / Downloads: 89)
Reply
#2
The attachment you provide, is your python script, not the excel files.
Reply
#3
(Aug-29-2021, 09:41 AM)shantanu97 Wrote: it is showing this error. Image Attached.
There is no image attached.

In you link with the .xlsm there is a problem with PEEL v2.0 UNLOCKED1.xlsm file.
When you ask people to test your code make it easier to do so,not use a path with your username in it.
C:\\Users\\ShantanuGupta\\Desktop\\Incoming\\Peel
import pandas as pd

fn = 'PEEL v2.0 UNLOCKED1.xlsm'
#fn = 'XtendTemplate_v125082021.xlsm' #Ok
df = pd.read_excel(fn, sheet_name=None, header=None, engine="openpyxl")
print(df)
Output:
{'Sheet1': Empty DataFrame Columns: [] Index: []}
If i open PEEL v2.0 UNLOCKED1.xlsm in excel an save it again then i get work.
Then i get both files to move to there folders(make my own path),
and there is no need to save if do nothing with files,just move them.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  saving and loading text from the clipboard with python program MaartenRo 2 91 Yesterday, 05:04 AM
Last Post: MaartenRo
  Increase the speed of a python loop over a pandas dataframe mcva 0 71 Jan-21-2022, 06:24 PM
Last Post: mcva
  Reading Excel file and use a wildcard in file name and sheet name randolphoralph 6 201 Jan-13-2022, 10:20 PM
Last Post: randolphoralph
  for loop in dataframe in pandas Paulman 7 439 Dec-02-2021, 12:15 AM
Last Post: bowlofred
  Showing and saving the output of a python file run through bash Rim 3 655 Oct-06-2021, 10:48 AM
Last Post: gerpark
  problem writing dataframe to oracle aliyesami 4 641 Sep-25-2021, 11:20 PM
Last Post: SamHobbs
  Structuring and pivoting corrupted dataframe in pandas gunner1905 2 556 Sep-18-2021, 01:30 PM
Last Post: gunner1905
  TypeError: 'DataFrame' object is not callable using Pandas in Python sofiavlachou 1 2,719 Sep-02-2021, 03:24 PM
Last Post: buran
  dataframe write to tab delimited differs from Excel koh 0 531 Aug-01-2021, 02:46 AM
Last Post: koh
  Verification of an excel file saleenahohn 2 613 Jul-09-2021, 06:56 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020