Python OpenPyxl - python

I'm working with a large excel file(30MB+,xlsm). Many of them contain photos in cell comments, so this file takes up so much space. First im open a file to read and get specific column name. Im open that with:
self.wb = load_workbook(str(self.directory[0]), read_only=True, keep_vba=True, data_only=False)
self.ws = self.wb['BAZA_PRACOWNIKOW']
self.sheet = self.wb.active
After finding the appropriate cells, I open it again to add the relevant data to it:
self.wb = load_workbook(str(self.directory[0]), read_only=False, keep_vba=True, data_only=False)
self.ws = self.wb['BAZA_PRACOWNIKOW']
self.sheet = self.wb.active
When I complete this file I would like to save it, so:
self.wb.save('new.xlsm')
After that i got an error:
D:\Anaconda\lib\site-packages\openpyxl\reader\worksheet.py:310:
UserWarning: Data Validation extension is not supported and will be
removed warn(msg)
The file size after saving decrease to 8mb. All photos in comments are not in new.xlsm file.
Can someone help me ?

http://openpyxl.readthedocs.io/en/latest/usage.html
openpyxl does currently not read all possible items in an Excel file
so images and charts will be lost from existing files if they are
opened and saved with the same name.
Try lxlm or xlwings maybe?

Related

openpyxl save without formats, python

I am getting an error when saving information in an excel sheet, I carry out the exercise of loading the workbook and I specify the sheet in which I will save the information and finally I use the .save method, this effectively saves the information I want but leaves me the raw file, as shown in the images
I am using version: openpyxl 3.0.10
def AsignacionUEM():
print("Cargando libro")
lPCartera = openpyxl.load_workbook("D:/PLANTILLA/PLANTILLA.xlsm",read_only = False, keep_vba = True)
print("Cargando hoja")
hPCartera = lPCartera.worksheets[10]
print("Agregando registros")
for row in expAsig:
hPCartera.append(row)
print("Guardando excel . . .")
lPCartera.save("D:/PLANTILLA/PLANTILLA_Envio.xlsm")
lPCartera.close()
*** Excel file with the formats before manipulating it with openpyxl ***
My Excel before
Excel file as a result of having manipulated it with openpxyl, this does save the information, but it leaves it without formats.
My Excel later
enter image description here

Touble with saving excel sheet/ making changes -- Python openpyxl

I am trying to automate the process of creating new cost sheets at my work.
I have a functional code that pulls the source file, renames it, moves it where I want it, etc.
I am trying to use openpyxl to write in some of the data that is parsed in creating the file/directory name. I can't posst my full ncode because it has work directories and such in it but here's the failing portion:
############################
#Write Info Into Cost Sheet#
############################
myworkbook=openpyxl.load_workbook(new_CSdst_file_name)
worksheet= myworkbook['COST SHEET']
worksheet['C3']= now.strftime("%m/%d/%Y")
worksheet['C5']='SOW-'+line_split[2]
worksheet['C6']='CS-'+line_split[4]
worksheet['C10']='0'
worksheet['J4']=line_split[3]
#myworkbook.save(new_CSdst_file_name)
If the save is commented out the file is created, but no cells are filled.
If I uncomment the save I get the attached excel error (see image)
Going home for the day, but any help appreciated. Will check in Monday!
Excel Error
Using #Greg 's comment I was able to find another answer that provided the solution.
Working code here:
myworkbook=openpyxl.load_workbook(new_CSdst_file_name, keep_vba=True)
worksheet= myworkbook['COST SHEET'] #Open Sheet Named COST SHEET (current Sheet 1 name in xlsm)
worksheet['C3']= now.strftime("%d-%b-%Y") #Date
worksheet['C5']='SOW-'+ PartNum #SOW number
worksheet['C6']='CS-'+ PartNum #CS number
worksheet['C7']= Description #descriptor
worksheet['C10']='0' #creating revision 0
worksheet['J4']= Customer #Company Name
myworkbook.save(new_CSdst_file_name)

Save Excel operation is not properly done

I am using custom Excel library which is based on openpyxl module
Following are the keywords:
def open_excel(self, file):
self.filename = file
self.wb = openpyxl.load_workbook(self.filename)
def write_data_by_coordinates(self,sheetname,row_value, column_value,varValue):
self.sheet = self.wb.get_sheet_by_name(sheetname)
self.row = int(row_value)
self.column = int(column_value)
self.varValue = varValue
self.sheet.cell(row=self.row, column=self.column).value = self.varValue
def save_excel(self, file):
self.file = file
self.wb.save(self.file)
Using Robot Framework, I’m trying to write the values in excel and save it as below:
Write Data in Excel
Open Excel D:\\TestExcel.xlsx
Write Data By Coordinates Sheet1 1 1 Test1
Write Data By Coordinates Sheet1 1 2 Test2
Save Excel D:\\TestExcel.xlsx
After execution, I had opened the excel and verified it,the values are updated in excel.
When I tried to close the excel , it shows pop up as “Do you want to Save”?
Why is this pop-up shown when I did not edit/modify any value in excel sheet?
Is it not saved properly during execution by the keyword “Save excel” keyword?
I had even tried by adding the below line to the Save excel method in Custom library as
self.wb.close()
But still it shows the “Do you want to Save?” popup, when I open and close the excel sheet after test case execution.
Any suggestions would be helpful
I think this is just that the excel programme see changes it did not make and thus treats it like you did them at this time of its opening.
You could test this by making an excel file that has never been opened on your PC. Edit with the code and then open with excel.

Inserting multiple images in Excel using OpenPyxel does not work

I'm trying to insert images with OpenPyxl to a Excel file that has already images in it (in different sheets in my case). If I do so, the existing images disappear.
Sample code:
import openpyxl
from openpyxl import load_workbook
wb = openpyxl.Workbook()
ws1 = wb.create_sheet("MySheet1")
img1 = openpyxl.drawing.image.Image('test1.png')
img1.anchor = ws1.cell(row=2, column=2).coordinate
ws1.add_image(img1)
wb.save('test_output.xlsx')
wb = load_workbook(filename='test_output.xlsx')
ws2 = wb.create_sheet("MySheet2")
img2 = openpyxl.drawing.image.Image('test2.png')
img2.anchor = ws2.cell(row=2, column=2).coordinate
ws2.add_image(img2)
wb.save('test_output.xlsx')
Is there anything I do wrong here?
Thanks alot in advance.
Update:
As stated in the comments this should work now:
Up until very recently images in existing files were not preserved.
You need >= 2.5.5 for this.
The doc states for older versions:
openpyxl does currently not read all possible items in an Excel file
so images and charts will be lost from existing files if they are
opened and saved with the same name.
There are several bug issues about this (here, here, here)
One comment in the issues is using win32com as a workaround to copy the sheet with the image to a different file.

Data not present in excel sheet

I'm reading a existing excel file by using openpyxl package and trying to save that file it, and it got saved but after opening that excel file no data is present. I used the following code and my requirement is to open the file in use_iterators = True mode only
from openpyxl import load_workbook
wb = load_workbook(filename = 'large_file.xlsx', use_iterators = True)
ws = wb.get_sheet_by_name(name = 'big_data')
for row in ws.iter_rows():
for cell in row:
print cell.internal_value
wb.save("large_file.xlsx")
can u guys show how to save the file and close the file after saving with out losing the data
Try loading with use_iterators = False, as use_iterators = True loads the data information differently, such that it may not contain all the information you wish to save.
Openpyxl writes and entirely new excel file based on the information it has read in, so it's not like you make a small change and just update the file. (This also means if certain features aren't supported in openpyxl (such as VB macros), these won't exist in the file you've saved.)

Categories