I am doing a fairly simple Excel I/O from several input sheets to one master sheet. The output is to be printed to one master sheet, in a book containing several other sheets. For some reason, while the input and printing is happening without any issues, the non-text (charts and images) of the other sheets in the book are being cleared. This is not what is intended, I just wanted to print the data directly into the sheet and leave everything else as it is.
At the top:
import openpyxl
from openpyxl import *
This is the code that I'm using to open the output workbook.
#locate and open the master book and sheet
master_sheet_location = os.path.join(directory,master_sheet)
wb = load_workbook(master_sheet_location)
sheets = wb.sheetnames
Sheet1 = wb[sheets[1]]
After printing the data, I save it with the following line of code:
wb.save(master_sheet_location)
The data are then read and printed iteratively to Sheet1. Is there something I'm not doing to preserve the rest of the sheets? I'm not dealing with the other sheets at all in the code.
Related
Short description of the problem:
I am currently accessing an Excel workbook from Python with openpyxl.
I have some dynamic spill formulas in sheet1, like filter(), byrow() and unique().
With the python script, I am doing some operations in sheet2, but I am not touching sheet1 (where the dynamic spill formulas are located).
When using workbook.save() method in Python, I experience that the dynamic formulas in sheet1 are ruined and static, not having the dynamic functionality they had before interacting with python.
What can I do? Use a parameter in .save()? Use another method?
Detailed description of problem (with pictures):
I have a workbook called Original, with the following three sheets:
nums
dynamic
dump
In "nums" I have a cell for ID (AA), and a column with some numerical values (picture1).
In "dynamic" I have some dynamic formulas like byrow() and filter() that updates automatically with the values in ID and Values-column of "nums" (picture2).
The sheet "dump" is for now empty.
I have a second workbook called Some_data, which have one sheet with a 3-column dataframe (picture3).
I am dumping the 3-column dataframe of Some_data into the empty "dump"-sheet of Original with a Python script, and then using the workbook.save() method to save the new workbook.
The code is here:
import pandas as pd
from openpyxl import load_workbook
Some_data = filepath of the workbook
Original = filepath of the workbook
df = pd.read_excel(Some_data, engine = "openpyxl")
wb = load_workbook(filename = Original)
ws = wb["dump"]
rownr = 2
for index, row in df.iterrows():
ws["B"+str(rownr)] = row["col1"]
ws["C"+str(rownr)] = row["col2"]
ws["D"+str(rownr)] = row["col3"]
rownr+=1
wb.save(filepath of new workbook)
Now, the newly saved workbook's sheet "dump" has now been populated.
The problem is that the dynamic formulas in the sheet "dynamic" has been ruined, although the python script does not interact with any of the sheets "nums" or "dynamic".
First of all - the dynamic array formulas (like filter) now have brackets around them (picture4), and the dynamic array formulas are not dynamic anymore (there are no blue line around the array when selected, and they do not update automatically; picture5).
I need help with what to do. I want to save the excel-file, but with the dynamic array formulas not being ruined.
Thank you for your help, in advance.
Frode
I have problem - I created an excel file through xlsxwriter library but now I have one porblem. How to import to this file sheet from another excel file?
Here is piece of code:
SHEET_NAME = 'Foreign_holders'
workbook = xlsxwriter.Workbook('Holdres.xlsx')
def create_sheet(workbook):
worksheet = workbook.add_worksheet(name=SHEET_NAME)
The problem is that I need to import all info from the sheet with all data and formulas from another excel file, which name, for example 'Holdres2.xlsx'. Because when I just copy it and paste on Holdres.xlsx - it just removed copied sheet and replaced with empty after I run the code.
I am trying to import excel files which have multiple sheets. Currently, my code (below) is only importing the first sheet. The remainder of the code is preforming calculations from only one sheet (currently the first since I moved it there to make it work-but bonus if I can avoid this step).
Ideally, I would like to import all the sheets, preform calculations on the one sheet, and export all sheets again in an excel file. A majority of the sheets would be import/export with no changes, while the one sheet with a specific/consistent name would have calculations preformed on it and also exported. Not sure what functions to look into. Thanks!
df = pd.read_excel("excelfilename.xlsx")
df.head()
#other code present here preforming calculations
df.to_excel(r'newfilename.xlsx', index = False)
Load Excel file using pandas, then get sheet names using xlrd, and then save modified data back.
import xlrd
file_name = "using_excel.xlsx"
sheet_names_ = xlrd.open_workbook(file_name, on_demand=True).sheet_names()
for sheet_name in sheet_names_:
df_sheet = pd.read_excel(file_name, sheet_name=sheet_name)
# do something
if you_want_to_write_back_to_same_sheet_in_same_file:
writer = pd.ExcelWriter(file_name)
df_sheet.to_excel(writer, sheet_name=sheet_name)
writer.save()
I need to read this .xlsm database and some of the cells values I need are derived from Excel functions. To accomplish this I used:
from openpyxl import load_workbook
wb = load_workbook('file.xlsm', data_only=True, keep_vba=True)
ws = wb['Plan1']
And then, for every cell I wanted to read:
ws.cell(row=row, column=column).value
This works fine for getting the data out. But the problem comes with saving. When I do:
wb.save('file.xlsm')
It saves the file, but all the formulas inside the sheets are lost
My dilemma is reading the cell's displayed values on one of the database's sheet without modifying them, writing the code's output in a new sheet and saving it.
Read the file once in read-only and data-only mode to look at the values and another time keeping the VBA around. And save under a different name.
I have created a basic workbook with one worksheet and in this worksheet I have created a table (Insert>Table). Nothing complex in this table, just the value 1, 2, 3 (and the column header of course).
I have written this simply code
import openpyxl
thefilename = r"C:\Users\Myfile.xlsx"
book = openpyxl.load_workbook(thefilename)
book.activesheet
book.save(thefilename)
Then, when I try to open the excel file, the file is corrupted and impossible to reopen it again.
This looks like a bug but I wonder how I can detect if my excel file has a table and how to remove it ?