简体   繁体   中英

Can I modify specific sheet from Excel file and write back to the same without modifying other sheets using Pandas | openpyxl

I'll try to explain my problem with an example:

Let's say I have an Excel file test.xlsx which has five tabs (aka worksheets): Sheet1, Sheet2, Sheet3, Sheet4 and sheet5. I am interested to read and modify data in sheet2.

My sheet2 has some columns whose cells are dropdowns and those dropdown values are defined in sheet4 and sheet5. I don't want to touch sheet4 and sheet5. (I mean sheet4 & sheet5 have some references to cells on Sheet2).

I know that I can read all the sheets in excel file using pd.read_excel('test.xlsx', sheetnames=None) which basically gives all sheets as a dictionary(OrderedDict) of DataFrames.

Now I want to modify my sheet2 and save it without disturbing others. So is it posibble to do this using Python Pandas library.

[UPDATE - 4/1/2019]

I am using Pandas read_excel to read whatever sheet I need from my excel file, validating the data with the data in database and updating the status column in the excelfile.

So for writing back the status column in excel I am using openpyxl as shown in the below pseudo code.

import pandas as pd
import openpyxl

df = pd.read_excel(input_file, sheetname=my_sheet_name)
df = df.where((pd.notnull(df)), None)

write_data = {}

# Doing some validations with the data and building my write_data with key 
# as (row_number, column_number) and value as actual value to put in that 
# cell.

at the end my write_data looks something like this: 
{(2,1): 'Hi', (2,2): 'Hello'}

Now I have defined a seperate class named WriteData for writing data using openpyxl

# WriteData(input_file, sheet_name, write_data)

book = openpyxl.load_workbook(input_file, data_only=True, keep_vba=True)
sheet = book.get_sheet_by_name(sheet_name)

for k, v in write_data.items():
   row_num, col_num = k
   sheet.cell(row=row_num, column=col_num).value = v

book.save(input_file)

Now when I am doing this operation it is removing all the formulas and diagrams. I am using openpyxl 2.6.2

Please correct me if I am doing anything wrong! Is there any better way to do?

Any help on this will be greatly appreciated :)

To modify a single sheet at a time, you can use pandas excel writer:

sheet2 = pd.read_excel("test.xlsx", sheet = "sheet2")

##modify sheet2 as needed.. then to save it back:

with pd.ExcelWriter("test.xlsx") as writer:
        sheet2.to_excel(writer, sheet_name="sheet2")

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