Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code Proposal: Allow User to Specify Sheet (Work with multi-sheet Excel Files) #1

Closed
bai-yi-bai opened this issue Jul 8, 2021 · 3 comments · Fixed by #2
Closed
Assignees
Labels
enhancement New feature or request

Comments

@bai-yi-bai
Copy link
Collaborator

bai-yi-bai commented Jul 8, 2021

This library is awesome! I'm using it a lot since it makes working with Excel files very simple.

I have a proposal: Allow the user to specify the sheet they want to modify.

I write a lot of functional programming and I can't remember if it's possible to overwrite a class function at import, so instead I've modified the sheet2dict to return a specified sheet when openpyxl opens the file.

in main.py:

def xlsx_to_dict(self, path, select_sheet=None):
        """
        Read a Worksheet and return it as array of dictionaries
        :param self: Worksheet Object
        :param path: Path to XLSX file
        :return: Array of rows as dictionaries
        """
        book = load_workbook(path)
        if select_sheet == None:
              sheet = book.active
        else:
              sheet = book[select_sheet]

The user would then read an Excel file like this:

ws =Worksheet()
ws.xlsx_to_dict(path=<insert path>, select_sheet='<sheetname>'

I don't know if this is the most elegant way to select a sheet, but it works for me.
I suppose to import all worksheets by default - I think the maximum is 32 - into a larger dictionary, <edit I didn't complete my thought>, that there could be a clever way to put dictionaries in dictionaries, but this might be too convoluted since I assume most users will just use one sheet.
Another option is just to continue creating Worksheet() objects with different names and sheets selected.

@Pytlicek Pytlicek self-assigned this Jul 8, 2021
@Pytlicek Pytlicek added the enhancement New feature or request label Jul 8, 2021
@Pytlicek
Copy link
Owner

Pytlicek commented Jul 9, 2021

Actually, this is a good idea @bai-yi-bai! I also thought about it a long time ago :)
I think the style in which you wrote the example code is fine 👍
I tested it and this way we wouldn't break anything in the code ⚡️
If you want to do PR, please do it and we can then merge it into sheet2dict.
Many thanks 🙏

Screenshot 2021-07-09 o 19 45 47

@bai-yi-bai
Copy link
Collaborator Author

Cool!

I also created another test Excel file for testing and everything looked fine. I think I created the pull request correctly from PyCharm/Github on my personal PC. I'm excited to replace the version on my work PC.

BTW, I found my way here from the post on HackerNews a few weeks ago.

@Pytlicek Pytlicek linked a pull request Jul 11, 2021 that will close this issue
@Pytlicek
Copy link
Owner

@bai-yi-bai thank you very much for your contribution to the sheet2dict 🙏

There is a new version https://github.com/Pytlicek/sheet2dict/releases/tag/0.1.0 released on PyPi with your changes: https://pypi.org/project/sheet2dict/

Thanks again and keep up the great work and contributions to the Open-Source Software 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants