wb_write = openpyxl.load_workbook(file_path)
first_sheet = wb_write.get_sheet_names()[0]
ws = wb_write.get_sheet_by_name(first_sheet)
#here row=1 ,column= 5
ws.cell(row=i, column=mt_mac_id_column).value = MT_MAC
wb_write.save(file_path)
After writing data into xlsx file and saved in the same workbook.
Opened manually xlsx file in windows Changes are reflected in xlsx file but while closing the xlsx file it is asking for confirmation like "do you want to save the changes you made to file"
Why it is asking for confirmation? How do I avoid this warning?
Related
I've created with tkinter an app that help me analyze a .txt file and save information in a excel file. The app works (no problem in saving or opening the file and is readable). I save my variable in the file using openpyxl this code:
from openpyxl import load_workbook
[...]
def excel(self):
wb = load_workbook('F:\\Python\\template\\excel_template.xlsx')
sheet = wb.active
sheet.cell(row=1, column=1).value = "HADS"
sheet.cell(row=2, column=1).value = HADS1
sheet.cell(row=3, column=1).value = HADS2
sheet.cell(row=1, column=2).value = "date"
sheet.cell(row=2, column=2).value = date1
sheet.cell(row=3, column=2).value = date2
[...]
filename = "F:\\Python\\file\\" name + "_" + year + ".xlsx"
wb.save(filename)
When I tried to upload it in my Djano website I got various error. I thought the problem could it be the separetor and so I opened my excel file with a text editor and this is what I found inside:
PK ôTAMb ± docProps/app.xmlMŽ=1DÿÊq½·AÁBb#ÐR°²{/dC²B~¾9ÁnoFß
g*â©-†Tã"’ Š¶N]§n—h¥cy ;ç‘ÎŒÏHI`«Ô¨ ¥™æMþŽFŸrxNæê±pe'Ã¥!
ÿrmÞ©Ô5ï&õ–Öð;i^PK ôTÌS2±ì Ç docProps/core.xml•ÑMK1à¿RrßM×Ò€(žTŠ·LÛÅÍÉÈÖïvm·•zñ˜™wžŒ4Q˜ð9…ˆ‰Ì“k}&.Ù–(
€l¶èt.û„ï›ëœ¦þ™6µùЄiUÍÁ!i«IÃ,â(²iÍHÆÏÔ€5€-:ô”—NYÂäòŸCgLîr3¦º®+»zÈõqx{|x–/ŸI{ƒLIk„I¨)$u㟴„³‚<üûS#;éuA_—ìØyoïV÷LM+>+ªyQÍVüZð…¨¯Þ÷Ö¯ùè‚mÖÍ?ÅÅ™x”„‹›©oPK ôT:€Ý—‡ c. xl/theme/theme1.xmlíZOoÛ6¿÷Sº»–dý±‹º…-Ûí–¤-š´C´L[l(Ñ ©$FQ ×]è†]ì¶Ã0 ÀvÚý6-¶ØW%ÿ‰dK6ím“Æ‚„äû½Çß{zï‘Ö¿¾¾yû,$à1ŽiÔÔŒëºPäÓŽFMíÑQ¯R× 0#B#ÔÔ&ˆk·o]» oˆ …Hñˆß€M-b|£Z徆ü:£HÎ
)¡ÿ²QuÀà©„
Usually when I open an Excel file with my text editor I would find my variables normally written. I'm doing something wrong while saving my file? Is there another way? I really don't need the template and I would be fine saving the file in a .csv format if this could be help solve the problem.
Thank you all for the help!
i know this question has been asked many times , i have read through the answers of previously asked question however im still not getting how to open the file
what i want to do is i have some data in df that i want to save in the existing excel sheet df2 which is password procted
df2 = pd.read_excel(r'C:\Users\RTambe00000\Desktop\python basics\web scraping\IEDriverServer_Win32_4.0.0\Data Miner Data.xlsx', sheet_name='data (1)')
df2 = df2.merge(df, left_on='Created', right_on ='Preferred Call Time')
im getting this below error
XLRDError: Can't find workbook in OLE2 compound document
i have solved my issue and was able to open the password protected excel file. Below is my code:-
import win32com.client
xl = win32com.client.gencache.EnsureDispatch('Excel.Application')
wb = xl.Workbooks.Open('File path', False, False, None, 'Read passward','Edit passward')
xl.Visible=1
time.sleep(5)
# to refresh the file
wb.RefreshAll()
time.sleep(5)
wb.Save()
xl.Quit()
Right now I am doing the following.
import xlrd
resp = requests.get(url, auth=auth).content
output = open(r'temp.xlsx', 'wb')
output.write(resp)
output.close()
xl = xlrd.open_workbook(r'temp.xlsx')
sh = 1
try:
for sheet in xl.sheets():
xls.append(sheet.name)
except:
xls = ['']
It's extracting the sheets but I don't know how to read the file or if saving the file as an .xlsx is actually working for macros. All I know is that the code is not working right now and I need to be able to catch the data that is being generated in a macro. Please help! Thanks.
I highly recommend using xlwings if you want to open, modify, and save .xlsm files without corrupting them. I have tried a ton of different methods (using other modules like openpyxl) and the macros always end up being corrupted.
import xlwings as xw
app = xw.App(visible=False) # IF YOU WANT EXCEL TO RUN IN BACKGROUND
xlwb = xw.Book('PATH\\TO\\FILE.xlsm')
xlws = {}
xlws['ws1'] = xlwb.sheets['Your Worksheet']
print(xlws['ws1'].range('B1').value) # get value
xlws['ws1'].range('B1').value = 'New Value' # change value
yourMacro = xlwb.macro('YourExcelMacro')
yourMacro()
xlwb.save()
xlwb.close()
Edit - I added an option to keep Excel invisible at users request
I am writing a dataframe to a range of Excel file in a certain tab, but after saving the file, I see that the Excel file has become unusable. Could anyone suggest a solution?
import openpyxl as pyx
df3_xmax= df3.iloc[0]
wb = pyx.load_workbook(dst)
xl_writer = pd.ExcelWriter(dst, engine='openpyxl')
xl_writer.book = wb
xl_writer.sheets = {ws.title:ws for ws in wb.worksheets}
df3_xmax.to_excel(xl_writer, 'shname', index=False, header=False, startcol=3, startrow=7)
xl_writer.save()
for sheet_name in book.sheet_names():
for index in range(len(tabs)):
tab = tabs[index]
if sheet_name == tab:
dump_file_name = dump_files[index]
dump_file_name = file_prefix+dump_file_name
sheet = book.sheet_by_name(sheet_name)
new_book = Workbook()
sheet1 = new_book.add_sheet("Sheet 1")
for row in range(sheet.nrows):
values = []
for col in range(sheet.ncols):
sheet1.write(row,col,sheet.cell(row,col).value)
xlsx_file_name = dirname+"/"+dump_file_name+".xlsx"
sheet1.title = xlsx_file_name
new_book.save(xlsx_file_name)
The file is creating and data is there, but if I open it in openoffice.org and click the save button it asks for new name.
The file can not be read by PHP also. Again if I open and save it with new name then it works perfectly. I think we have to add something in the code so that it could be used by PHP.
i did google and found the solution here
http://xlsxwriter.readthedocs.org/getting_started.html
This is exactly what i wanted.
Creating and saving files to xlsx format.
Now its working perfectly.
original source
How to save Xlsxwriter file in certain path?
important link:
https://pypi.python.org/pypi/PyExcelerate