简体   繁体   English

在python中的同一工作簿中创建Excel工作表

[英]Creating Excel sheets in same workbook in python

Need a suggestion in my code. 需要在我的代码中提出建议。

I have a data frame in sheet1 of workbook: 我在工作簿的sheet1中有一个数据框:

column 1           column 2
000A0000              2
000B0000              3
000A0001              5
000B0001              1

My desired result: 我想要的结果:

in sheet 2 of Workbook: 在工作簿的第2页中:

column 1           column 2
 000A0000              2
 000A0001              5

In sheet 3 of Workbook: 在工作簿的表3中:

column 1           column 2
 000B0000              3
 000B0001              1

I have done my coding: 我完成了我的编码:

import pandas as pd
file="workbook.xlxs"
print(data.sheet_names)
data=data.parse("sheet1")

substrings = ['A', 'B']

T = {x: df[df['sheet1'].str.contains(x, na=False, regex=False)] for x in substrings]

for key, var in T.items():
    var.to_excel(f'{key}.xlsx', index=False)

by this I can create new workbook. 通过这个我可以创建新的工作簿。 But I need to create new worksheet in same workbook. 但是我需要在同一个工作簿中创建新的工作表。

Any suggestion would be appreciated. 任何建议将不胜感激。

To add sheets to the same excel file use openpyxl module as follows: 要将工作表添加到同一个Excel文件,请使用openpyxl模块,如下所示:

import pandas as pd
import openpyxl

#reading the sheet1 using read_excel
df = pd.read_excel('workbook.xlsx', sheet_name='Sheet1')

#creating pandas ExcelWriter object and loading the excel file using `openpyxl`    
df_writer = pd.ExcelWriter('workbook.xlsx', engine='openpyxl')
excel = openpyxl.load_workbook('workbook.xlsx')
df_writer.book = excel

#checking string in column 1 and writing those to respective sheets in same workbook
for string in ['A','B']:
    df[df['column 1'].str.contains(string)].to_excel(df_writer,sheet_name=string)
#saving and closing writer
writer.save()
writer.close()

to_excel would not append sheets to your existing file: to_excel不会将工作表附加到现有文件:

use openpyxl instead:(something like below) 改用openpyxl :(如下所示)

import pandas
from openpyxl import load_workbook

book = load_workbook('path+filename_you_want_to_write_in.xlsx')
writer = pandas.ExcelWriter('path+filename_you_want_to_write_in.xlsx', engine='openpyxl') 
writer.book = book
writer.sheets = dict((ws.title, ws) for ws in book.worksheets)

df.to_excel(writer, "Sheet_name_as_per_your_choice",index=False)

writer.save()

Also if you dynamically want to read through the sheets and not specific sheets: 此外,如果您动态地想要阅读工作表而不是特定工作表:

f = pd.ExcelFile(file)
sheet_names = df.sheet_names
for i in list(sheet_names):
    df = pd.read_excel(f,i)

This iterates through all your sheets and provides a dataframe based on the sheets. 这将遍历所有工作表,并根据工作表提供数据框。

Try using the xlsxwriter engine. 尝试使用xlsxwriter引擎。

writer = pd.ExcelWriter('<< file_name >>', engine='xlsxwriter')

df.to_excel(writer, sheet_name='Sheet2')

writer.save()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python for Google Sheets:将数据框写入同一工作簿中的不同工作表 - Python for Google Sheets: write dataframes to different sheets in the same workbook Python,如何将不同的 excel 工作簿合并为一个 excel 工作簿作为工作簿 - Python, how to combine different excel workbooks into one excel workbook as sheets 在工作表之间创建超链接,它们都在同一工作簿中 - Creating Hyperlink between sheets where they all are in same workbook 从Python中创建Excel中的新工作簿 - Creating a new workbook in Excel from Python breaks 从Excel Workbook中的表格动态生成Python列表 - Dynamicaly Build Python lists from Sheets in Excel Workbook 将工作簿中的多个 Excel 工作表合并为一张工作表 Python - Combine Multiple Excel sheets within Workbook into one sheet Python 通过Python在Excel工作簿中的多个工作表中添加单元格值 - Adding cell values from multiple sheets in Excel workbook via Python 将来自不同excel文件的工作表连接到一个工作簿python中 - Joining sheets from different excel files into one workbook python Python - Excel - 将工作表添加到现有工作簿而不删除工作表 - Python - Excel - Add sheet to existing workbook without removing sheets Python pyexcelerate 库将多个工作表写入同一个工作簿? - Python pyexcelerate library writing multiple sheets to same workbook?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM