I received the following error as I tried to open the Excel file which I have saved using openpyxl:
Excel could not open excel_test.xlsx because some content is unreadable. Do you want to open and repair this workbook?
Is there a way to tackle this issue without having to click the "repair" option? I have looked into here but it does not solve the problem.
The original content of the file looks like this:
and here is my code:
import openpyxl
path_excel = ""
workbook_test = openpyxl.load_workbook(filename = path_excel)
worksheet_test = workbook_test["Sheet1"]
for row_, cellObj_ in enumerate(worksheet_test["D"], 1):
if row_ == 1:
cellObj_.value = "SUM"
else:
cellObj_.value = "= $B${0} + $C${0}".format(row_)
workbook_test.save(filename = path_excel)
Related
I'm currently trying to do the following:
Open up an .xml file that's already in spreadsheet format with Excel
Save the .xml file as .xlsx without corrupting the file
Other options that I can take via Python are:
Convert the .xml to .xlsx
Copy specific columns (A1:AC6000) to another Excel workbook
Import an XML file directly in an Excel workbook.
I failed at all of them and can't think of a different way so here I am asking for help. My latest code is here:
# importing openpyxl module
import openpyxl as xl;
# opening the source excel file
file = 'C:\\Users\\ddejean\\Desktop\\HESKlogin\\Downloads\\data.xlsx'
wb1 = xl.load_workbook(file)
ws1 = wb1['Sheet1']
# opening the destination excel file
filename1 = 'C:\\Users\\ddejean\\Desktop\\HESKlogin\\Downloads\\updated.xlsx'
wb2 = xl.load_workbook(filename1)
ws2 = wb2['Sheet1']
# calculate total number of rows and
# columns in source excel file
mr = ws1.max_row
mc = ws1.max_column
# copying the cell values from source
# excel file to destination excel file
for i in range (1, mr + 1):
for j in range (1, mc + 1):
# reading cell value from source excel file
c = ws1.cell(row = i, column = j)
# writing the read value to destination excel file
ws2.cell(row = i, column = j).value = c.value
# saving the destination excel file
wb2.save(filename1)
I also tried changing the format of the file which ultimately corrupted the file:
A = r"C:\\Users\\ddejean\\Desktop\\HESKlogin\\Downloads\\data.xml"
pre, ext = os.path.splitext(A)
B = os.rename(A, pre + ".xlsx")
I tried importing the file into Excel which was terrible since none of the data in xml have properly name attributes to differentiate the data. I also tried calling a macro, but I get an error with each macro on my network, so I disposed of that alternative.
Any assistance you can give would be much appreciated! I also think it's important to say that I'm a noob.
This works for me :)
import os
import win32com.client as win32
import requests as r
import pandas as pd
hesk = "C:\\Users\\ddejean\\Desktop\\TEST\\hesk.xml"
folder = "C:\\Users\\ddejean\\Desktop\\TEST"
output = "C:\\Users\\ddejean\\Desktop\\TEST\\output.csv"
cd = os.path.dirname(os.path.abspath(folder))
xmlfile = os.path.join(cd, hesk)
csvfile = os.path.join(cd, output)
# EXCEL COM TO SAVE EXCEL XML AS CSV
if os.path.exists(csvfile):
os.remove(csvfile)
try:
excel = win32.gencache.EnsureDispatch('Excel.Application')
wb = excel.Workbooks.OpenXML(xmlfile)
wb.SaveAs(csvfile, 6)
wb.Close(True)
except Exception as e:
print(e)
finally:
# RELEASES RESOURCES
wb = None
excel = None
I'm making a Python program that opens, edits, and saves xlsm Excel files with VBA. However, in this line wb.save(filename = 'b.xlsm'), the following error is occurring: lxml.etree.XMLSyntaxError: Opening and ending tag mismatch: br line 19 and b, line 20, column 12.
I must edit and save the xlsm file.
import openpyxl
wb = openpyxl.load_workbook(filename = 'a.xlsm', keep_vba = True)
ws = wb.active
ws.cell(1,1).value = 'test'
print(ws.cell(1,1).value)
wb.save(filename = 'b.xlsm')
Gonna guess something is broken in another part of your code, as this aren't 20 lines and the code on itself runs just fine on my machine :) Based on the error code, you are just missing a bracket somewhere
I am trying to convert an Excel file to an HTML file while keeping the format of the workbook.
Using Excel, I am able to switch from xlsx to htm: File -> Save as -> Web page (*.html, *.htm)
Using Python, I am always getting something gibberish like the below image as workbook.htm or workbook.html.
import xlwings as xw
file_path = "*.xlsx"
excel_app = xw.App(visible=False)
wb = excel_app.books.open(file_path)
wb.save("*.html")
wb.save("*.htm")
from xlsx2html import xlsx2html
xlsx2html('*xlsx', '*.htm')
xlsx2html('*xlsx', '*.html')
I have used dummy files, I am just trying to go from the xlsx file to the htm/hmtl file using Python and keeping the format, e.g. background colors, borders, etc.
I used to have such problem. I also used xlwings library, customized it and success. You find and edit in the file xlwings/_xlwindows.py as follows:
def save(self, path=None):
saved_path = self.xl.Path
source_ext = os.path.splitext(self.name)[1] if saved_path else None
target_ext = os.path.splitext(path)[1] if path else '.xlsx'
if saved_path and source_ext == target_ext:
file_format = self.xl.FileFormat
else:
ext_to_file_format = {'.xlsx': FileFormat.xlOpenXMLWorkbook,
'.xlsm': FileFormat.xlOpenXMLWorkbookMacroEnabled,
'.xlsb': FileFormat.xlExcel12,
'.xltm': FileFormat.xlOpenXMLTemplateMacroEnabled,
'.xltx': FileFormat.xlOpenXMLTemplateMacroEnabled,
'.xlam': FileFormat.xlOpenXMLAddIn,
'.xls': FileFormat.xlWorkbookNormal,
'.xlt': FileFormat.xlTemplate,
'.xla': FileFormat.xlAddIn,
'.html': FileFormat.xlHtml # ---> add new
}
i have a weekly report that i need to do, i chooseed to create it with openpyxl python module, and send it via mail, when i open the received mail (outlook), the cells with formulas appears as empty, but when downloading the file and open it, the data appears, OS fedora 20. parts of the code :
# imported modules from openpyxl ...
wb = Workbook()
ws = wb.active
counter = 3
ws.append(row)
for day in data :
row = ['']*(len(hosts)*2 +5)
row[0] = day.dayDate
row[1] ='=SUM(F'+str(counter)+':'+get_column_letter(len(hosts)+5)+str(counter)+\
')/(COUNT(F'+str(counter)+':'+get_column_letter(len(hosts)+5)+str(counter)+'))'
row[2] = '=SUM('+get_column_letter(len(hosts)+6)+str(counter)+':'+\
get_column_letter(len(hosts)*2+5)+str(counter)+')/COUNT('+\
get_column_letter(len(hosts)+6)+str(counter)+':'+\
get_column_letter(len(hosts)*2+5)+str(counter)+')'
row[3] = '=MAX('+get_column_letter(len(hosts)+6)+str(counter)+':'+\
get_column_letter(len(hosts)*2+5)+str(counter)+')'
row[4] = '=_xlfn.STDEV.P('+get_column_letter(len(hosts)+6)+str(counter)\
+':'+get_column_letter(len(hosts)*2+5)+str(counter)+')'
counter += 1
then, i create from the date some charts, etc.. and save, then send via mail :
wb.save(pathToFile+fileName+'.xlsx')
os.system('echo -e "'+msg+'" | mail -s "'+fileName+'" -a '+\
pathToFile+fileName+'.xlsx -r '+myUsr+' '+ppl2send2)
those are parts of the actual code, any one have an idea why the email don't show the results of the formulas in the cells ? Thanks in advance :)
openpyxl doesn't compute a result for formulas inserted into a spreadsheet; if you open the sheet with excel and save it, the result will have values filled in.
opepyxl has a problem with formulas, after you update your excel file you need to open it and save it to get the values generated. There are two ways to solve this problem.
(I won't advice you to use this unless you really want it.)
You can automate the process of opening the file and saving it from python before reading it. You can do this by using the win32com module
import win32com.client
wb.save('PUT YOUR FILE PATH HERE')
ab = win32com.client.Dispatch("Excel.Application")
wb2 = ab.Workbooks.Open('PUT YOUR FILE PATH HERE')
ws = ab.Sheets('PUT THE SHEET NAME HERE')
ab.DisplayAlerts = False
wb2.Save()
wb2.Close()
ab.Application.Quit()
#Now you can read from the file and you can see the values generated from the formula
Or you can use xlwings instead of openpyxl. if you use this module you don't have to worry about saving the excel file. The module will do it for you.
import xlwings
wb= xlwings.Book('PUT YOUR FILE PATH HERE')
ws = wb.sheets[0]
#Do your update here example ws.range(2, 8).value = 34
wb.save()
wb.close()
I'm using formulas:
wsheet.write(i, j, Formula('HYPERLINK(%s;"Link")' % click), docnm)
in my Excel files
and when it first opens up it goes into "Protected View". My formulas don't load until after I click "Enable Editing". Is there anyway to get my numbers to show up even if Protected Mode is on?
I found a similar topic on this link Protected View in Microsoft Excel 2010 and Python , but there aren't any useful answers..
Can someone help me please?
Complete code:
from xlwt import easyxf,Formula
import xlwt
wbook = xlwt.Workbook()
wsheet = wbook.add_sheet("MySheet")
wsheet.col(j).width = 17000
link="https://stackoverflow.com/"
click="http://ccwebviewer.ac.de.eu.ericsson.se/~"+excelbranch+link
click='"'+str(click)+'"'
linkName='"'+"LINK"+'"'
wsheet.write(1, 1, Formula('HYPERLINK(%s;%s)' % (click,linkName)))
wbook.save("excel.xls")
I have tried a basic example with the code below and seem to be able to open the workbook produced without a prompt and follow the link:
import xlwt
link_url = 'http://stackoverflow.com/questions/21430921/disable-protected-view-mode-in-excel-files-with-xlwt-python' #'file1.csv'
outputfile = 'outputList.xls'
wbk = xlwt.Workbook()
wsheet = wbk.add_sheet('sheet 1')
xlformula = 'HYPERLINK("'+link_url+'", "Link")'
wsheet.write(0, 0, xlwt.ExcelFormula.Formula(xlformula))
wbk.save(outputfile)
This basically creates a new workbook and writes a link to the URL for this question and saves the workbook.