简体   繁体   中英

Creating 3 tables into excel using Openpyxl

I am trying to create 3 tables in an excel file, however they are created one below the other. I am looking to place them next to each other. I suspect append is causing the tables to be created under each other. Can you please help me!

sh2.append(["Market Sector", "Assets (in GBP)"])
for column in small:
    sh2.append(column)
tab = Table(displayName="Small_Companies", ref="A1")

sh2.append(["Market Sector", "Assets (in GBP)"])
for column in medium:
    sh2.append(column)
tab = Table(displayName="Mid_Companies", ref="D1")

sh2.append(["Market Sector", "Assets (in GBP)"])
for column in large:
    sh2.append(column)
tab = Table(displayName="Mid_Companies", ref="G1")

A simple solution is to use zip to combine three lists and append them all together. I don't know if these three lists are of same size so I first append them to same length. Also your call to Table function is not quite right, the ref parameter requires a range of the table. I just assume you have 5 rows.

max_length = max(max(len(small), len(medium)), len(large))
small += [""] * (max_length - len(small))
medium += [""] * (max_length - len(medium))
large += [""] * (max_length - len(large))
sh2.append(["Market Sector", "Assets (in GBP)","Market Sector", "Assets (in GBP)","Market Sector", "Assets (in GBP)"])
for (c1,c2,c3) in zip(small,medium,large):
    sh2.append(c1+c2+c3)
tab1 = Table(displayName="Small_Companies",ref="A1:C5")
tab2 = Table(displayName="Mid_Companies",ref="D1:F5")
tab3 = Table(displayName="Large_Companies", ref="G1:I5")
sh2.add_table(tab1)
sh2.add_table(tab2)
sh2.add_table(tab3)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM