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
Related
i am trying to copy an excel spreadsheet which has some text and numbers and a logo image into multiple excel files as a new sheet with source formatting using python, any help is greatly appreciated.
It's not clear what your end requirement and restrictions are.
The basic requirement;
"I am trying to copy an excel spreadsheet which has some text and numbers and a logo image into multiple excel files"
just indicates you want multiple copies of the same Excel file, the File Management app of your OS can do this, with the limitation perhaps being the resultant naming of each file. If this is your requirement then perhaps something like
Python - making copies of a file
may help to create the files with the necessary naming.
If its a workbook with multiple sheets and you only want one sheet copied to new workbooks then openpyxl can help. Copying sheets is easy enough however the formatting requires extra code.
If there is just a couple of sheets in the original it may be easier to just remove those sheets before saving your copy, see example code below.
Otherwise this link may help
How to copy worksheet from one workbook to another one using openpyxl?
Example:
The following code shows how to open an existing workbook, 'templateA.xlsx' which has two sheets, 'Sheet1 & 'Sheet2'. You only want 'Sheet1' saved as multiple copies (10) named 'template1.xlsx', 'template2.xlsx', 'template3.xlsx'...
The code open the original workook, deletes the sheet called 'Sheet2' before making 10 copies.
from openpyxl import load_workbook
# Load the original Excel workbook
wb = load_workbook("templateA.xlsx")
# delete the Sheet2 that is not required
del wb["Sheet2"]
# Save 10 copies of the workbook with Sheet1 only
for i in range(10):
wb.save("template" + str(i) + ".xlsx")
The first link python-making-copies-of-a-file can help if your required naming is more complex than the example given.
openpyxl
I'm trying to write some code that will change sheet names in an excel file based on the data in another excel file.
At first this worked fine;
Sheet_names = "A","B","C","D"
wb = openpyxl.load_workbook(file_name.xlsx)
for i in range (1, len(Sheet_names)):
Name_change = wb["Sheet{}".format(str(i))]
wb.active = Name_change
Name_change.title = "{}".format(Sheet_names[i])
wb.save((file_name.xlsx))
wb.close()
But for some reason (I'm unsure what I've changed) a graph within the excel file isn't updating. So the data reference is still to Sheet1, Sheet2 etc.
Im also getting a warning message that there are external links - I guess it's assuming the sheet references are external? The excel file comes from a template that's copied across.
Changing it manually isn't an option, Is having Openpyxl recreate the graph for every sheet the only option?
Out of ideas, help!
I have a excel file which has specific format, font and color etc. I would like to read it using Python libraries like pandas (pd.read_excel) and just modify a few cells without affecting the style. Is it possible? Currently, when I read and write using pandas, the style changes and it seems difficult to make the complex style in Python again.
Is there a way to load and store the format/style of Excel file when we are reading it, to be applied when it is being saved? I just want to modify the value of few cells.
You can use the openpyxl library like this:
from openpyxl import Workbook, load_workbook
workbook = load_workbook("test.xlsx") # Your Excel file
worksheet = workbook.active # gets first sheet
for row in range(1, 10):
# Writes a new value PRESERVING cell styles.
worksheet.cell(row=row, column=1, value=f'NEW VALUE {row}')
workbook.save("test.xlsx")
You can use the set_properties() function.
Further use can be viewed at How to change the font-size of text in dataframe using pandas
I Completely have no idea where to start.
I want to edit some think like:
To:
I want to save the result in a .txt file.
Every thing i know is to open and read the file.
code:
import pandas as pd
file = "myfile.xlsx"
f = pd.read_excel(file)
print(f)
I think the image colors speak for themselves how the code have to run. If not, I'll answer any question.
My go-to for editing Excel spreadsheets is openpyxl
I don't believe it can turn .csv or .xlsx/xlsm into .txt files, but it can read .xlsx/xlsm and save them as a .csv, and pandas can read csv files, so you can probably go from there
Quick example:
from openpyxl import load_workbook
wb = load_workbook("foo.xlsx")
sheet = wb["baz"]
sheet["D5"] = "I'm cell D5"
Use openpyxl, and look at this below:
Get cell color from .xlsx
color_in_hex = sh['A2'].fill.start_color.index # this gives you Hexadecimal value of the color (in cell A2)
So you'd have to iterate across your columns/rows checking for a colour match, then if its a match, grab the value and apply it to your new sheet
I want to save excel sheet with existing sheet formats and background color.
I could save successfully to a new file with out old file format.
How can I keep the existing excel sheet format when save in to a new file.
writer = pd.ExcelWriter('New.xlsx', engine='xlsxwriter')
dfDiff.to_excel(writer, sheet_name='DIFF', index=False)
writer.save()
I had no good experience with pandas and format styles... what works if you use win32com like this example:
from win32com.client import DispatchEx
excel = DispatchEx('Excel.Application')
wbP=excel.Workbooks.Open(r'C:\Temp\Junk\Temp.xlsx')
wbG=excel.Workbooks.Open(r'C:\Temp\Junk\Temp2.xlsx')
wbG.Worksheets('TAA2').Copy(Before=wbP.Worksheets("TAA"))
wbP.SaveAs(r'C:\Temp\Junk\Temp.xlsx')
excel.Quit()
del excel # ensure Excel process ends
this copies everything... (styles, formats, formulas etc.)
Another option is copy the whole workbook and delete the not needed sheets, if you create a new file and not editing an existing one...
Copy worksheet from one workbook to another one using Openpyxl