How to Split Each Excel Sheet Into a Separate File Using Python

Поделиться
HTML-код
  • Опубликовано: 7 янв 2022
  • 👉 Explore All My Excel Solutions: pythonandvba.com/solutions
    𝗗𝗘𝗦𝗖𝗥𝗜𝗣𝗧𝗜𝗢𝗡
    ▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀
    In this tutorial, we will learn how to split each Excel sheet into a separate file and put them on a new and separate directory.
    🌍 𝗟𝗜𝗡𝗞𝗦:
    ▶ GitHub Repo: github.com/Sven-Bo/split-exce...
    𝗧𝗢𝗢𝗟𝗦 𝗔𝗡𝗗 𝗥𝗘𝗦𝗢𝗨𝗥𝗖𝗘𝗦
    ▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀
    🆓【𝗙𝗥𝗘𝗘】Excel Add-in (𝗠𝘆𝗧𝗼𝗼𝗹𝗕𝗲𝗹𝘁): pythonandvba.com/mytoolbelt
    📊 Dashboard Excel Add-In (𝗚𝗿𝗮𝗳𝗹𝘆): pythonandvba.com/grafly
    🎨 Cartoon Charts Excel Add-In (𝗖𝘂𝘁𝗲𝗣𝗹𝗼𝘁𝘀): pythonandvba.com/cuteplots
    🤪 Fun Emoji Excel Add-In (𝗘𝗺𝗼𝗷𝗶𝗳𝘆): pythonandvba.com/emojify
    📑 Excel Templates: pythonandvba.com/go/excel-tem...
    🎓 My Courses: pythonandvba.com/go/courses
    📚 Books, Tools, and More: pythonandvba.com/resources
    𝗖𝗢𝗡𝗡𝗘𝗖𝗧 𝗪𝗜𝗧𝗛 𝗠𝗘
    ▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀
    🔗 LinkedIn: / sven-bosau
    📸 Instagram: / codingisfun_official
    💻 GitHub: github.com/Sven-Bo
    💬 Discord: pythonandvba.com/discord
    📬 Contact: pythonandvba.com/contact
    ☕ 𝗕𝘂𝘆 𝗺𝗲 𝗮 𝗰𝗼𝗳𝗳𝗲𝗲?
    If you want to support this channel, you can buy me a coffee here: pythonandvba.com/coffee-donation

Комментарии • 57

  • @CodingIsFun
    @CodingIsFun  2 года назад +2

    *I will be here in the comments section. Please provide your exact error message for any issues, and I will try to help.*

    • @faisalmehmood6889
      @faisalmehmood6889 2 года назад

      Excellent work! I need to compare the two data frames (two workbooks) for reconciliation purposes. For each column, the difference should be shown in the new workbook (difference should be identified column by column)

    • @CodingIsFun
      @CodingIsFun  2 года назад +1

      @@faisalmehmood6889 Thank you! Interesting question. Have a look at the new pandas method 'compare' introduced with version 1.1.0:
      pandas.pydata.org/docs/reference/api/pandas.DataFrame.compare.html
      I hope this helps!

    • @faisalmehmood6889
      @faisalmehmood6889 2 года назад +1

      @@CodingIsFun thank you

    • @quality6299
      @quality6299 Год назад

      split excel file into multiple excel files using python but doesn't use pandas library and file access from path i put in script and output path also i put in script....so i use xlwt and xlrd library but am not getting perfect result...please help....

  • @tarekelias463
    @tarekelias463 2 года назад

    Really good and simple explanation, thanks a lot! Just used it in my work to save a lot of time

    • @CodingIsFun
      @CodingIsFun  2 года назад

      Fantastic! Thanks for watching and your comment! :)

  • @ianpropst-campbell6028
    @ianpropst-campbell6028 2 года назад

    Thank you for this video. The code worked perfectly.

    • @CodingIsFun
      @CodingIsFun  2 года назад +1

      Great to hear! And thank you for taking the time to leave a comment!

  • @Jriad
    @Jriad 2 года назад

    and to be honest your videos are great and to the point

    • @CodingIsFun
      @CodingIsFun  2 года назад

      Glad you like the videos! And thanks for the time to leave a comment! :)

  • @rajeevmenon1975
    @rajeevmenon1975 2 года назад

    Awesome Bro. You rock

  • @viper1424
    @viper1424 2 года назад

    Thanks for this

    • @CodingIsFun
      @CodingIsFun  2 года назад

      You are welcome! Thanks for watching the video! :)

  • @EmekKov
    @EmekKov 2 года назад

    Hi, this is amazing. Would it be possible to use this method (of course after some modifications) to split one sheet into workbooks based on column content?

    • @CodingIsFun
      @CodingIsFun  2 года назад

      Thanks, do you mean something like this?
      👉 ruclips.net/video/NWD2LSSuyjE/видео.html

  • @colinhuang2325
    @colinhuang2325 2 года назад +1

    Hi Sven, is that possible that we can write multiple DataFrames to the same sheet which already exists?

    • @CodingIsFun
      @CodingIsFun  2 года назад

      Hi Colin Huang. That is possible - have a look at the following StackOverflow post:
      stackoverflow.com/a/20221655

  • @FredyArg
    @FredyArg 2 года назад

    Thanks for this, do you have a sample where we have a user
    1st be prompted for the directory,
    2nd select a file.
    3rd, split the excel file into separate excel files based on the name of a column within the excel file?

    • @CodingIsFun
      @CodingIsFun  2 года назад +1

      Thanks for watching. Have a look at the following video: ruclips.net/video/_H5hsUwv8lE/видео.html
      With that, you could cover questions 1 and 2.
      For the 3rd question, you might need to do further customization on your side. Happy Coding!

    • @FredyArg
      @FredyArg 2 года назад

      @@CodingIsFun hey thanks i tried it and gives me the following error
      import PySimpleGUI as sg
      df = sg.popup_get_file("Choose Excel File", multiple_files=False,file_types=(("Excel Files","*.xls*"), ),)
      if not df:
      sg.popup("Cancel", "No filse name supplied")
      raise SystemExit("Cancelling: No File Name Supplied")
      else:
      sg.popup("The File Name you selected was:", df)
      df.head()
      ---------------------------------------------------------------------------
      AttributeError Traceback (most recent call last)
      in
      ----> 1 df.head()
      AttributeError: 'str' object has no attribute 'head'

  • @chyang0107
    @chyang0107 5 месяцев назад

    Appreciate your work! Your insights would be appreciated if to share how to split Excel sheets into Excel files using Python. For instance, my master Excel file has 5 sheets (Hank-1, Hank-2, Hank3, Mary -1, and Mary-2). After executing the ''work-saving" script, I will have two resulting Excel files such as Hank.xlsx (Hank-1, Hank-2, Hank3) and Mary.xlsx (Mary-1 and Mary-2). Many thanks!

    • @CodingIsFun
      @CodingIsFun  4 месяца назад +1

      Thank you very much for watching the video and your comment. Your requirements are well noted. Yet, I receive many requests for creating individual solutions. As much as I want to help, I simply do not find the time in my daily schedule to develop & test all the different requests. I hope you can understand. Thank you!

    • @chyang0107
      @chyang0107 4 месяца назад

      @@CodingIsFunthanks for your reply! I figured out the solution by your script on how to merge the worksheets to workbook. By using your script, I filtered these file names by Mary or Hank first then merge these files( Mary-1, Mary-2,…). Many thanks!

  • @AnkitKumar-jr7rx
    @AnkitKumar-jr7rx 2 года назад

    Hello sir i want to know how can we connect jupyter notebook to excel for trading purpose. I want to place order in excel and it will send this order to jupyter notebook (python code) for execution.

    • @CodingIsFun
      @CodingIsFun  2 года назад

      I would suggest the following:
      * Hook up Python with Excel using xlwings. Check out this video: ruclips.net/video/iIATJtruZBE/видео.html
      * Use your trading API in the Python script
      * Execute the Python script via Excel using xlwings (as shown in the video)
      I hope this helps!

  • @rajwolbajracharya
    @rajwolbajracharya 2 года назад

    How can we convert this into an executable file asking option for input folder and output folder?

    • @CodingIsFun
      @CodingIsFun  2 года назад

      Thanks for watching the video & your question. I will make a separate tutorial about it. I will publish the video this weekend. Stay tuned! 👍

    • @CodingIsFun
      @CodingIsFun  2 года назад +1

      Here you are: ruclips.net/video/_H5hsUwv8lE/видео.html
      Happy Coding!

  • @jedenzemenemsow
    @jedenzemenemsow Год назад

    great tutorial! any chance you could show us how to split similar file by rows, let's say we have 1000 rows in a file, and we would like to split it into 10 files each one with the header and 100 rows of data?

    • @CodingIsFun
      @CodingIsFun  Год назад

      Thank you! Regarding your request, what would be the trigger to split the files? Then the number of rows has reached 100, or should it be based on the column value?

  • @charlesthamba4514
    @charlesthamba4514 2 года назад

    Just AWESOME Sir.
    Plz I want to know, in Vba how can I open a new excel workbook and work on it when a userform is already is display? Plz help. Thank you for all

    • @CodingIsFun
      @CodingIsFun  2 года назад

      Thanks!
      I am not sure if I understood your question.
      To open a workbook in VBA, you could write:
      Sub OpenWorkbook()
      Workbooks.Open "C:\Users\Username\Desktop\myFile.xlsx" 'Change file path
      'Do something here
      End Sub

    • @charlesthamba4514
      @charlesthamba4514 2 года назад

      @@CodingIsFun Thanks for your reply Sir.
      I meant "in Excel, when a userform is already shown, you cannot open a new workbook and work on it. So, how to give this possibility even userform shown ?"

    • @CodingIsFun
      @CodingIsFun  2 года назад

      @@charlesthamba4514 Alright, but I do not know how to do that out of the box. You might need to google for a solution. Sorry that I cannot help you with that!

  • @Jriad
    @Jriad 2 года назад

    Great work!
    but i think it'd have been more practical to output to folders same as the folder name it was in

    • @CodingIsFun
      @CodingIsFun  2 года назад

      Thanks!
      You just need to change the output path to "file.parent". Have a look here:
      www.screencast.com/t/Iiwyoa41WMm
      I hope this helps!

  • @benjaminjohnson6936
    @benjaminjohnson6936 Год назад

    This does exactly what I want it to (in theory)! but I can't get it to work. On a fresh Python install (3.11) on Windows, with xlwings installed via pip, and only changing the INPUT_DIR, I get the following error:
    Traceback (most recent call last):
    File "[PATH]/Documents/Python Scripts/split_wb.py", line 13, in
    with xw.App(visible=False) as app:
    File "C:\Users\[USER]\AppData\Local\Programs\Python\Python311\Lib\site-packages\xlwings\main.py", line 294, in __init__
    self.impl = engines.active.apps.add(
    AttributeError: 'NoneType' object has no attribute 'apps'

    • @CodingIsFun
      @CodingIsFun  Год назад

      Which xlwings version are you using? Please refer to the requirements in the GitHub repo

    • @benjaminjohnson6936
      @benjaminjohnson6936 Год назад

      @@CodingIsFun I'm an idiot. I somehow had multiple Python installs competing with PATH. Fully uninstalled/reinstalled everything and it's working fine now. Thank you!

  • @riazahmedshaik
    @riazahmedshaik 4 месяца назад

    Hi , This is fantastic. I have tabs like France_1,France_2,France_3, India_1,India-2,_indai-3 in a excel work book. I need to get generate a excel book for each country with tabs. Eg: France_1,France-2,France-3 one excel book and for India one excel book with tabs. How to do it? Please help

    • @CodingIsFun
      @CodingIsFun  3 месяца назад

      Thank you very much for watching the video and your comment. Your requirements are well noted. Yet, I receive many requests for creating individual solutions. As much as I want to help, I simply do not find the time in my daily schedule to develop & test all the different requests. I hope you can understand. Thank you!

  • @saifshaikh344
    @saifshaikh344 Год назад

    can i use openpyxl for this ?

    • @CodingIsFun
      @CodingIsFun  Год назад

      If you want to preserve the formatting -> No

  • @ranyfahed9926
    @ranyfahed9926 2 года назад

    greate video but when i try to run the script it crash and display this error : with xw.App(visible = False) as app .....AttributeError:__enter__

    • @CodingIsFun
      @CodingIsFun  2 года назад

      Please use the latest xlwings version 'pip install xlwings --upgrade' and try again. Thanks!

    • @ranyfahed9926
      @ranyfahed9926 2 года назад

      @@CodingIsFun its already the latest version and still not working --- version xlwings-0.27.3

    • @CodingIsFun
      @CodingIsFun  2 года назад

      @@ranyfahed9926 that's interesting. Then I also do not know why you are getting this error.

    • @ranyfahed9926
      @ranyfahed9926 2 года назад

      i think there is a bug in the version because so the script will not work

    • @CodingIsFun
      @CodingIsFun  2 года назад

      @@ranyfahed9926 Please create a separate environment, clone the repo, install the requirements.txt and try again. If you are still facing any issue, then I would call it a bug 😉
      github.com/Sven-Bo/split-excel-sheet-into-workbook

  • @quality6299
    @quality6299 Год назад

    hi...Your videos is best but its not my my scenario
    solution..How i split the one excel file into multiple excel files by particular row name...

    • @CodingIsFun
      @CodingIsFun  Год назад

      Thanks for watching. Something like this? ruclips.net/video/NWD2LSSuyjE/видео.html

    • @quality6299
      @quality6299 Год назад

      @@CodingIsFun Without pandas its happened or not????

    • @CodingIsFun
      @CodingIsFun  Год назад

      @@quality6299 sorry, but I don't understand your question