write xlsx file using openpyxl module in python - python

I am having trouble writing to excel file using openpyxl module. So far I am able to write this code
from openpyxl.workbook import Workbook
import datetime
header = [u'Name', u'Email', u'Mobile', u'Current location',]
new_data = [
[u'name1', u'email1#yahoo.com', 9929283421.0, u'xxxx'],
[u'name2', u'email2#xyz.com', 9994191988.0, u'xxxx']
]
wb = Workbook()
cur_date = str(datetime.date.today())
log_file = "%s/%s_%s_errorlog.xlsx" % (settings.MEDIA_ROOT,
os.path.splitext(file_name)[0],
cur_date)
log_csv = wb.worksheets[0]
for i in range(1, len(header) + 1):
log_csv.cell(row = 1 ,column = i).value = header[i - 1]
wb.save(log_file)
error_count = 0
for each_row in new_data:
error_count += 1
for i in range(1, len(each_row) + 1):
log_csv.cell(row = error_count ,column = i).value = each_row[i - 1]
wb.save(log)
File is created, but it is corrupted and I am not able to open it with the excel file reader (LibreOffice) provided by the OS (ubuntu). Also the contents of the file are not readable. Not sure what I am doing wrong

from openpyxl.workbook import Workbook
header = [u'Name', u'Email', u'Mobile', u'Current location',]
new_data = [[u'name1', u'email1#yahoo.com', 9929283421.0, u'xxxx'],
[u'name2', u'email2#xyz.com', 9994191988.0, u'xxxx']]
wb = Workbook()
dest_filename = 'empty_book.xlsx'
ws1 = wb.active
ws1.title = "range names"
ws1.append(header)
for row in new_data:
ws1.append(row)
wb.save(filename = dest_filename)
I am able to write the content to xlsx like above.

Related

How to update existing Excel .xlsx file using Openpyxl?

I am trying to make this code work, the problem is that in the excel file the print (...) data is not written, I have tried to use ws.appened (...) but without results.
import arcpy
from openpyxl import Workbook
wb = Workbook("C:/Users/Hp/Desktop/ejemplo/VINCULACION_S.xlsx")
ws = wb.active
rows = arcpy.SearchCursor("C:/Users/Hp/Desktop/ejemplo/VH_Dissolve.shp",
fields="COLOR; INTERNO_DE; CLASE_DEMA; COUNT_AREA; SUM_AREA; SUM_LENGTH",
sort_fields="COLOR 222; INTERNO_DE A")
# COLOR, INTERNO_DE, CLASE_DEMA, COUNT_AREA, SUM_AREA y SUM_LENGTH.
for row in rows:
print("Color: {0}, Interno: {1}, Clase:{2}, ContarA: {3}, SumarA: {4}, SumarL: {5}".format(
row.getValue("COLOR"),
row.getValue("INTERNO_DE"),
row.getValue("CLASE_DEMA"),
row.getValue("COUNT_AREA"),
row.getValue("SUM_AREA"),
row.getValue("SUM_LENGTH")))
wb.save('VINCULACION_S.xlsx')
I have also tried to locate the results data in the excel file from cell B3: G3 onwards but I can't find it.
I got this code as a solution to the post
import arcpy
import openpyxl as px
def main():
wb = px.load_workbook(r"C:\Users\Hp\Desktop\Ejemplo\VINCULACION_S.xlsx")
ws = wb['VINCULACION_SH_NUE']
in_features = r"C:\Users\Hp\Desktop\Ejemplo\VH_Dissolve.shp"
row_num = 3
with arcpy.da.SearchCursor(
in_features,
["COLOR", "INTERNO_DE", "CLASE_DEMA", "COUNT_AREA", "SUM_AREA", "SUM_LENGTH"],
) as cursor:
for row in cursor:
ws.cell(row=row_num, column=2).value = row[0]
ws.cell(row=row_num, column=3).value = row[1]
ws.cell(row=row_num, column=4).value = row[2]
ws.cell(row=row_num, column=6).value = row[3]
ws.cell(row=row_num, column=7).value = row[4]
ws.cell(row=row_num, column=8).value = row[5]
row_num += 1
wb.save(r"C:\Users\Hp\Desktop\Ejemplo\VINCULACION_S.xlsx")
if __name__ == "__main__":
main()
Try deleting the content in the existing sheet. Save and reload it.
import arcpy
from openpyxl import Workbook
wb = Workbook("C:/Users/Hp/Desktop/ejemplo/VINCULACION_S.xlsx")
ws = wb.active
k = 100 # approximate no of rows in existing sheet
for i in range(k):
ws.delete_rows(0)
wb.save()
wb = load_workbook("C:/Users/Hp/Desktop/ejemplo/VINCULACION_S.xlsx")
ws = wb.active

Merging two .xlsx files

I copy the content from a .xlsx file to another .xlsx file.
Openpyxl can't handle headerimages, so i create a .xlsx File with xlsxwriter including the header image and then copy the content with Openpyxl to the second .xlsx file. Works fine but openpyxl delete the headerimage. How to keep the Image in the header?
This is my code so far:
import openpyxl as xl
from openpyxl.styles import Font, PatternFill, Alignment
from openpyxl import load_workbook
import xlsxwriter
logo = "logo.jpg"
########################################################
## Create new sysfile with xlsxwriter
########################################################
workbook = xlsxwriter.Workbook('new_sysfile.xlsx')
#Insert Worksheets
worksheet1 = workbook.add_worksheet('Sheet1')
worksheet2 = workbook.add_worksheet('Sheet2')
worksheet3 = workbook.add_worksheet('Sheet3')
cell_format = workbook.add_format()
cell_format.set_font_name('Arial')
cell_format.set_font_size('11')
worksheet1.set_landscape()
worksheet2.set_landscape()
worksheet3.set_landscape()
header1 = '&L&G' + '&R hText '
worksheet1.set_margins(top=1)
worksheet1.set_header(header1, {'image_left': logo})
worksheet2.set_margins(top=1)
worksheet2.set_header(header1, {'image_left': logo})
worksheet3.set_margins(top=1)
worksheet3.set_header(header1, {'image_left': logo})
workbook.close()
#############################################################
# opening the source excel file
sourcefile = "sysfile2.xlsx"
wb1 = xl.load_workbook(sourcefile)
ws1 = wb1["Sheet1"]
ws2 = wb1["Sheet2"]
ws3 = wb1["Sheet3"]
# opening the destination excel file
dest_file = "new_sysfile.xlsx"
wb2 = xl.load_workbook(dest_file)
ws_dest1 = wb2["Sheet1"]
ws_dest2 = wb2["Sheet2"]
ws_dest3 = wb2["Sheet3"]
# some formatting
# calculate total number of rows and
mr1 = ws1.max_row
mc1 = ws1.max_column
mr2 = ws2.max_row
mc2 = ws2.max_column
mr3 = ws3.max_row
mc3 = ws3.max_column
# copying the cell values from source
for i in range(1, mr1 + 1):
for j in range(1, mc1 + 1):
# reading cell value from source excel file
c = ws1.cell(row=i, column=j)
# writing the read value to destination excel file
ws_dest1.cell(row=i, column=j).value = c.value
# SECOND SHEET
for i in range(1, mr2 + 1):
for j in range(1, mc2 + 1):
# reading cell value from source excel file
c = ws2.cell(row=i, column=j)
# writing the read value to destination excel file
ws_dest2.cell(row=i, column=j).value = c.value
# THIRD SHEET
for i in range(1, mr3 + 1):
for j in range(1, mc3 + 1):
# reading cell value from source excel file
c = ws3.cell(row=i, column=j)
# writing the read value to destination excel file
ws_dest3.cell(row=i, column=j).value = c.value
ws2.sheet_properties.pageSetUpPr.fitToPage = True
ws2.page_setup.fitToWidth = True
# (some formatting)
ws_dest1.sheet_properties.pageSetUpPr.fitToPage = True
ws_dest1.page_setup.fitToHeight = False
ws_dest2.sheet_properties.pageSetUpPr.fitToPage = True
ws_dest2.page_setup.fitToHeight = False
ws_dest3.sheet_properties.pageSetUpPr.fitToPage = True
ws_dest3.page_setup.fitToHeight = False
wb2.save(str(dest_file))
I hope someone have a solution.
Thank you.

Code to write appended data into xlsx rows without overwrite under a looping environment

I am going to write appended data into xlsx rows without overwrite under a looping environment. Unfortunately not working.
from openpyxl import Workbook
from openpyxl import load_workbook
t=0
while t <= 3:
filename = "myfile.xlsx"
d1 = 'ID:001'
d2 = 'ID:002'
d3 = 'ID:003'
new_row = [(d1), (d2), (d3)]
try:
wb = load_workbook(filename)
ws = wb.worksheets[0]
except FileNotFoundError:
headers_column = ['Header 1', 'Header 2', 'Header 3']
wb = Workbook()
ws = wb.active
ws.append(headers_row)
ws.append(new_row)
wb.save(filename)
wb.save.close()
t = t + 1
you have 2 errors:
from openpyxl import Workbook
from openpyxl import load_workbook
t=0
while t <= 3:
filename = "myfile.xlsx"
d1 = 'ID:001'
d2 = 'ID:002'
d3 = 'ID:003'
new_row = [(d1), (d2), (d3)]
try:
wb = load_workbook(filename)
ws = wb.worksheets[0]
except FileNotFoundError:
headers_row = ['Header 1', 'Header 2', 'Header 3']
wb = Workbook()
ws = wb.active
ws.append(headers_row)
ws.append(new_row)
wb.save(filename)
wb.close()
t = t + 1
you had a typo: headers_column should be headers_row.
and you had wb.save.close() but it should only be wb.close()

How to write multiple sheets into a new excel, without overwriting each other?

I'm trying to write multiple excels' column A into a new excel's column A (assuming all the excels have one worksheet each.) I've written some code, which can write one excel's column A into the new excel's column A; but if there are multiple excels, the new excel's column A will be overwritten multiple times. So how could I just add all the column As to the new excel sheet one after another without overwriting each other?
Below are my code:
import os, openpyxl
path = os.getcwd()
def func(file):
for file in os.listdir(path):
if file.endswith('.xlsx'):
wb = openpyxl.load_workbook(file)
sheet = wb.active
colA = sheet['A']
wb = openpyxl.Workbook()
r = 1
for i in colA:
sheet = wb.active
sheet.cell(row=r, column=1).value = i.value
r += 1
wb.save('new.xlsx')
func(file)
Thank you so much!!
you could proceed for example as:
import os, openpyxl
path = os.getcwd()
def func(outputFile):
c = 0
#create output workbook
wbOut = openpyxl.Workbook()
sheetOut = wbOut.active
for fName in os.listdir(path):
if fName.endswith('.xlsx'):
c += 1 #move to the next column in output
wb = openpyxl.load_workbook(fName)
sheet = wb.active #input sheet
#for r in range(1, sheet.max_row+1):
# sheetOut.cell(row=r, column=c).value = sheet.cell(row = r, column = 1).value
for r, cell in enumerate(sheet['A']):
sheetOut.cell(row = r+1, column = c).value = cell.value
wbOut.save(outputFile)
#"concatenate" all columns A into one single column
def funcAppend(outputFile):
wbOut = openpyxl.Workbook()
sheetOut = wbOut.active
r = 1
for fName in os.listdir(path):
if fName.endswith('.xlsx'):
wb = openpyxl.load_workbook(fName)
sheet = wb.active
for cell in sheet['A']:
sheetOut.cell(row = r, column = 1).value = cell.value
r += 1
wbOut.save(outputFile)
func('test.xlsx')

How to convert multiple xlsx sheet to csv using python

I can able to convert xlsx to csv in the case of single excel sheet.
How can i do the same in the case of multiple sheet in single excel file?
I have tried:
workBook = xlrd.open_workbook(filePath)
sheet_names = workBook.sheet_names()
lenth = len(sheet_names)
for i in range(0,lenth):
sheet = workBook.sheet_by_name(sheet_names[i])
yourcsvFile = open(csvPath, 'wb')
wr = csv.writer(yourcsvFile, quoting=csv.QUOTE_ALL)
for rownum in xrange(sheet.nrows):
wr.writerow(sheet.row_values(rownum))
yourcsvFile.close()
Try this
import sys
import xlrd
import csv
filePath = sys.argv[1] # user input file
csvPath = sys.argv[2]
workBook = xlrd.open_workbook(filePath)
sheet_names = workBook.sheet_names()
list_sheet = []
lenth = len(sheet_names)
for i in range(0,lenth):
sheet = workBook.sheet_by_name(sheet_names[i])
list_sheet.append(sheet)
yourcsvFile = open(csvPath, 'wb')
wr = csv.writer(yourcsvFile, quoting=csv.QUOTE_ALL)
total_row = list_sheet[0].ncols
for k in xrange(0,1):
for rownum in xrange(list_sheet[k].nrows):
wr.writerow(list_sheet[k].row_values(rownum))
if len(sheet_names) > 1:
for k in xrange(1,len(list_sheet)):
if list_sheet[k].ncols != total_row:
continue
for rownum in xrange(1,list_sheet[k].nrows):
wr.writerow(list_sheet[k].row_values(rownum))
yourcsvFile.close()

Categories