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
Related
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.
I'd like to copy the formatting between two Excel sheets in python.
Here is the situation:
I have a script that effectively "alters" (ie overwrites) an excel file by opening it using pd.ExcelWriter, then updates values in the rows. Finally, file is overwritten using ExcelWriter.
The Excel file is printed/shared/read by humans between updates done by the code. Humans will do things like change number formatting, turn on/off word wrap, and alter column widths.
My goal is the code updates should only alter the content of the file, not the formatting of the columns.
Is there a way I can read/store/write the sheet format within python so the output file has the same column formatting as the input file?
Here's the basic idea of what I am doing right now:
df_in= pd.read_excel("myfile.xlsx")
# Here is where I'd like to read in format of the first sheet of this file
xlwriter = pd.ExcelWriter('myfile.xlsx', engine='xlsxwriter')
df_out = do_update(df_in)
df_out.to_excel(xlwriter,'sheet1')
# Here is where I'd like to apply the format I read earlier to the sheet
xlwriter.save()
Note: I have played with xlsxwriter.set_column and add_format. As far as I can tell, these don't help me read the format from the current file
Pandas uses xlrd package for parsing Excel documents to DataFrames.
Interoperability between other xlsx packages and xlrd could be problematic when it comes to the data structure used to represent formatting information.
I suggest using openpyxl as your engine when instantiating pandas.ExcelWriter. It comes with reader and writer classes that are interoperable.
import pandas as pd
from openpyxl.styles.stylesheet import apply_stylesheet
from openpyxl.reader.excel import ExcelReader
xlreader = ExcelReader('myfile.xlsx', read_only=True)
xlwriter = pd.ExcelWriter('myfile.xlsx', engine='openpyxl')
df_in = pd.read_excel("myfile.xlsx")
df_out = do_update(df_in)
df_out.to_excel(xlwriter,'sheet1')
apply_stylesheet(xlreader.archive, xlwriter.book)
xlwriter.save()
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?
I am trying to add data to an existing excel file, the problem I am facing is that the data is getting imported but the equation and the format is being deleted in original file.
I attached my code below
import xlwt
import xlrd
from xlutils.copy import copy
#open the excel file
rb=xlrd.open_workbook('Voltage_T.xlsx')
#make a writable copy of the opened excel file
wb=copy(rb)
#read the first sheet to write to within the writable copy
w_sheet=wb.get_sheet(0)
#write or modify the value at 2nd row first column
w_sheet.write(0,1,'WWW.GOOGLE.COM')
#the last step saving the work book
wb.save('Voltage_WW.xls')
You need to set formatting_info to true
rb=xlrd.open_workbook('Voltage_T.xlsx', formatting_info = True)
However xlrd doesn't support xlsx with formatting_info at the moment. So if you really have to use .xlsx you will need another library.
I didn't used it myself so I can't tell you if it's a good library but thanks to a quick search on google XlsxWriter seems to answer your needs.
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.)