Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
tables in Excel
#1
Hi,

I have an Excel workbook with two sheets, called "Sheet1" and "Sheet2" with both 2 columns and 4 rows of data. They also have column headers. I would like to have a python script that creates a new sheet named "Sheet3" that puts the data from both sheets in a table that can be filtered and sorted. The tables should be sorted on age from low to high.

Any input is much appreciated!

Attached Files

.xlsx   Map1.xlsx (Size: 10.57 KB / Downloads: 40)
Reply
#2
That sounds fairly easy.

Read all about it here!

ws2 = wb.create_sheet("Mysheet", 0) # insert at first position
Then you can loop through the rows and columns of each sheet to copy values to another sheet.
Reply
#3
(Jan-06-2024, 06:35 AM)MaartenRo Wrote: Hi,

I have an Excel workbook with two sheets, called "Sheet1" and "Sheet2" with both 2 columns and 4 rows of data. They also have column headers. I would like to have a python script that creates a new sheet named "Sheet3" that puts the data from both sheets in a table that can be filtered and sorted. The tables should be sorted on age from low to high.

Any input is much appreciated!

I am not really an expert in working with excel files, but it would be something like this, I guess. It makes a new sheet and inserts sorted data into it. I am not sure if you need anything else on top of it. You need to install pandas and openpyxl packages for it.

import pandas as pd

df1 = pd.read_excel('Map1.xlsx', sheet_name='Sheet1')
df2 = pd.read_excel('Map1.xlsx', sheet_name='Sheet2')

combined_df = pd.concat([df1, df2])

combined_df = combined_df.sort_values(by='age')

with pd.ExcelWriter('Map1.xlsx', engine='openpyxl', mode='a') as writer:
    combined_df.to_excel(writer, sheet_name='Sheet3', index=False)
Reply
#4
The openpyxl documentation has an example of making a table.

https://openpyxl.readthedocs.io/en/lates...ables.html
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python script to summarize excel tables, then output a composite table? i'm a total n surfer349 1 2,354 Feb-05-2021, 04:37 PM
Last Post: nilamo
  Excel tables and move rows jdos 6 4,540 Jul-02-2020, 05:52 AM
Last Post: jdos
  Write tables from Word (.docx) to Excel (.xlsx) using xlsxwriter jackie 1 3,206 May-27-2020, 11:47 PM
Last Post: mcmxl22

Forum Jump:

User Panel Messages

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