Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Combining Spreadsheets
#1
Hello, everyone. I am new to Python and recently I tried to write a script inside the Python shell of my Windows 11 computer, in order to combine different spreadsheets; though when I run it I receive a syntax error, and I do not know why. Your help in solving this would be appreciated. I am running Python 3.12.1; and this is the code that I wrote:

import os
import pandas as pd

data_file_folder= "C:/Users/Py/Documents/Work/Portfolio"

df= []

for file in os.listdir(data_file_folder):
if file.endswith(".xlsx"): print("Python at work {0}...".format(file))
df.append(os.path.join(data_file_folder, file), sheet_name="data"))
deanhystad write Feb-02-2024, 07:22 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#2
What is the syntax error? I see some indentation errors, but that might be the result of not knowing how to post code (a mistake we all make the first time). Or is it the mismatched ")" at the end of the file?

What are you trying to do with this?
df.append(os.path.join(data_file_folder, file), sheet_name="data")
It is definitely not combining multiple spreadsheets into one big spreadsheet. For help with that you can look here.

https://www.geeksforgeeks.org/how-to-mer...th-python/
Reply
#3
I tried using the code detailed in the link suggested for my original post regarding this, but it also did not work: I received an error stating that "excl_merged" is undefined. Can someone help me with this? You can see the code I used, below:

import glob
import pandas as pandas

path = "C:/Users/Py/Documents/Employment/Portfolio"

file_list = glob.glob(path + "/*.xlsx")

excl_list=[]

for file in file_list:
    excl_list.append(pd.read_excel(file))

excl_merged = filepd.concat(excl_list, ignore_index=True)

excl_merged.to_excel('test.xlsx', index= False)
Reply
#4
Have you looked at more than one tutorial for this kind of operation?

As an example: how about this one

Myself, I'd be looking at using OpenPyXL for this kind of work and I have in fact used said (not for this task, but for a far simper one) and it can get very involved and code heavy, if you want to have all the correct checks in place, which you should, tbh.

edit to add: I'm not a pandas user, but I've just learned that as of pandas version 2.0.0, the append() method has been completely removed, so you may want to check your version and, if needs be, use the concat() method.
Sig:
>>> import this

The UNIX philosophy: "Do one thing, and do it well."

"The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse

"Everything should be made as simple as possible, but not simpler." :~ Albert Einstein
Reply
#5
Your code is nothing like the code in the example. You don't even import pandas which is the tool that reads and combines the spreadsheets.

Quote: I received an error stating that "excl_merged" is undefined
The posted code would not generate that error message. It would complain that filepd is undefined.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Merging spreadsheets with the same columns and extracting rows with matching entries johnbernard 3 9,936 Aug-19-2021, 03:08 PM
Last Post: johnbernard
Question How to make a 3D List of Excel Spreadsheets? chatguy 4 2,759 Jan-24-2021, 05:24 AM
Last Post: buran
  IDE with spreadsheets lewispgj 4 2,333 Mar-13-2020, 09:45 PM
Last Post: lewispgj
  opepyxl not counting rows in spreadsheets? jjartus 10 6,571 Mar-07-2020, 09:17 AM
Last Post: jjartus

Forum Jump:

User Panel Messages

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