I am opening a .xlsm file using openpyxl abd updated some cells in it and then saved as .xlsm file.Now when I open the saved file I see that the cells which were merged in the original file are broken in new file.
the code which i am using is-
from openpyxl import Workbook
from openpyxl import load_workbook
wb = load_workbook('Excel.xlsm',read_only=False ,keep_vba=True)
ws = wb['K0 Reg Patch Util']
ws.cell(row=42,column=3).value = 25
ws.cell(row=43,column=3).value = 30
ws.cell(row=44,column=3).value = 24
wb.save('Test.xlsm')
how to modify the code so that the file remains in the same format in the new saved file too. Right now with this code the cells which were merged in the original file are broken. How to resolve that because i dont want to destroy the structure in the original file.
I have tried saving the file with the same name then also the same problem is coming. Actually i have to save the file with the same structure but the updated values.Can you guys please help me with this??
Related
I need to open and edit my Excel with openpyxl, store the excel as a dataframe, and close the excel without any changes. Are there any ways to kill the excel and disable the auto-recovery dialogue which may pop out later?
The reason I'm asking is that my code worked perfectly fine in Pycharm, however after I packed it into .exe with pyinstaller, the code stopped working, the error said "Excel cannot access the file, there are serval possible reasons, the file name or path does not exist, or the file is being used by another program, or the workbook you are saving has the same name as a currently open workbook.
I assume it is because the openpyxl did not really close the excel, and I exported it to a different folder with the same file name.
Here is my code:
wb1 = openpyxl.load_workbook(my_path, keep_vba=True)
ws1 = wb1["sheet name"]
making changes...
ws1_df = pd.DataFrame(ws1.values)
wb1.close()
Many thanks ahead :)
The following way you can do this. solution
from win32com.client import Dispatch
# Start excel application
xl = Dispatch('Excel.Application')
# Open existing excel file
book = xl.Workbooks.Open('workbook.xlsx')
# Some arbitrary excel operations ...
# Close excel application without saving file
book.Close(SaveChanges=False)
xl.Quit()
I have an excel file that i want to use as template, what i need is just change the values of some cells, and save it as another excel file. The problem is that when i save it the formatting, style and some date values are changed
from openpyxl import load_workbook
wb = load_workbook("test.xlsx")
ws = wb["RDO"]
ws["B8"].value = "MAI MAN"
wb.save("new.xlsx")
The old file:
The new one:
As you can see the borders and date fields were changed.
I was thinking in just unzip the excel and modify the xml files, then zip it back, but this approach has a problem. I will need to make a copy of some worksheets, so i tought i should be ok in just copy and paste the sheet.xml file and change the workbook.xml file to add this new sheet, but when i do this all the cells are cleared which is weird because when i copy the sheet in the excel program the output sheet file it's exactly the same as the original
I would like some simple solution if possible, maybe some other library or a fix for this xml sheet problem
I'm new to programming and I'm trying to make a small app where I'll need to copy and paste values to a "stylished" excel woorksheet but when I was testing the first commands, I ran to the first issue:
When I get a cell value from "worksheet1" and paste it into an empty cell from the same worksheet ("worksheet1"), the worksheet stylish kinda messes up (just border cells disapearing), and thats not good, because that table is going to be printed and I need those border cells to stay.
I made a video with the issue: https://www.youtube.com/watch?v=DCZAwYp4zvE
I've tried:
Changing the files directory
Copying the excel file
Making the same but in the other woorksheets
I downloaded the original excel from internet so I though it was some incompatibility issue. I even created a new workbook and duplicated from 0 the original workbook (borders, same font, font size, etc), but didnt fix it.
Code:
from openpyxl import load_workbook
file="Test.xlsx"
wb = load_workbook(file)
ws = wb["COMPRA_DIRECTA"]
ws["A80"] = ws["A13"].value
wb.save(file)
Link to the original excel and the script: Link
Im using:
Excel 2016
Openpyxl 2.5.14
Windows
I have used the encoding and it is working fine for me. I have installed Openpyxl 2.6.4 and using LibreOffice Calc on Ubuntu 18.04.
Try this code, it will surely work for you:
from openpyxl import load_workbook
file="Test.xlsx"
wb = load_workbook(file)
# grab the active worksheet
ws = wb["COMPRA_DIRECTA"]
# Data can be assigned directly to cells
ws['A80'] = ws["A13"].value.encode("utf8")
# Save the file
wb.save("sample.xlsx")
I am currently working on a project that require me to write a pandas DataFrame to an EXISTING Macro Enabled Excel File (.xlsm), and then saved the file as a different (.xlsm) file.
EXISTING File: "Structure Report Template.xlsm", where the "Sheet1" contains a vba button. I want to write the DataFrame to "Sheet2".
I hope someone could help with the issue please, thank you in advance for your time.
I have tried to use [pd.to_excel], but it only supports '.xls' and 'xlsx' files. Using '.xlsm' extension will cause an error.
I also tried to create workbook object from openpyxl, and use _[workbook.add_vba_project('vbaProject.bin')]_, but it also causing the same error when I am trying to open the new excel file:
Excel cannot open the file 'myFilename.xlsm' because the file format or file extension is not valid. Verify > that the file has not been corrupted and that the file extension matches the format of the file.
import pandas as pd
import openpyxl
import shutil
from openpyxl import Workbook
from openpyxl import load_workbook
from openpyxl.utils.dataframe import dataframe_to_rows
template='Structure Report Template'
new_file_name='VGC Structure Report'
shutil.copy(template,new_file_name)
writer=pd.ExcelWriter(new_file_name+'.xlsm',engine='xlsxwriter')
vgc_endfile.to_excel(writer,sheet_name='Sheet2')
workbook=writer.book
workbook.filename=new_file_name+'.xlsm'
workbook.add_vba_project('vbaProject.bin')
writer.save()
wb=load_workbook(new_file_name+'.xlsm')
sheet2=wb['Sheet2']
for x in dataframe_to_rows(vgc_endfile):
sheet2.append(x)
wb.save(new_file_name+'.xlsm')
What I want to achieve:
open the 'Structure Report Template.xlsm'
write dataframe to 'Structure Report Template.xlsm' and "Sheet2"
Save 'Structure Report Template.xlsm' as 'VGC Structure Report.xlsm'
I am opening a .xlsm file using openpyxl abd updated some cells in it and then saved as .xlsm file.Now when I open the saved file I see that the cells which were merged in the original file are broken in new file.
the code which i am using is-
from openpyxl import Workbook
from openpyxl import load_workbook
wb = load_workbook('Excel.xlsm',read_only=False ,keep_vba=True)
ws = wb['K0 Reg Patch Util']
ws.cell(row=42,column=3).value = 25
ws.cell(row=43,column=3).value = 30
ws.cell(row=44,column=3).value = 24
wb.save('Test.xlsm')
Even on simple opening and saving file with openpyxl merged columns borders in the original file are broken. I have searched many times regarding this problem but none of the solutions were satisfying. I even came across a monkeypatch script which is to be included in the script after including the openpyxl library.The source for the script is-
https://bitbucket.org/openpyxl/openpyxl/issues/365/styling-merged-cells-isnt-working
The monkeypatch will overwrite the definition of merged cell from that present in the library.
can somebody tell me how to include this patch in the script and What does "self" means in the script.
I ran into a similar issue. But for me this only occurs with "protected" excel files. Removing that protection worked for me. No more "broken" merged cells. At least for files with .XLSX extension, I did not test .XLSM.