How to avoid overwriting of cell in excel using python? - python

I am using python-2.7 and xlsxwriter for writing in excel sheet.
Following is my code...
workbook = Workbook('D:\S_details.xlsx')
sheet = workbook.add_worksheet()
rownum = 2
colnum = 2
for a in student_result:
for r, row in enumerate(student_result):
for c, col in enumerate(row):
bold = workbook.add_format({'bold': 1})
sheet.write('A1','Student_ID',bold)
sheet.write('B1','Student_Link',bold)
sheet.write('C1','Student_Name',bold)
sheet.write('D1','Student_Qualification',bold)
sheet.write('E1','Student_Address',bold)
sheet.write('F1','Student_City',bold)
sheet.write('G1','Student_State',bold)
sheet.write('H1','Student_Country',bold)
sheet.write('I1','Student_Stream',bold)
sheet.write('J1','Student_Gender',bold)
sheet.write(r,c,col)
rownum = rownum + 1
colnum = colnum + 1
the code runs well but the very first entry which is retrieved from database is overwritten by the header of each column.
Hence only first entry is overwritten and rest of the entries are visible perfectly.
I am also printing the data before writing it to excel sheet but it is not showing any error nor the records are duplicated or so.
Can anyone please guide where I am going wrong...
Guidance / Help in any form is welcome.
Thank-you in advance :)

There are a few issues with the code example:
The headers are re-written for every iteration of the inner loop. This part of the code should be outside the loop.
The for a in student_result loop is unused.
The row_num and col_num variables are incremented but not used.
The enumerate() returns a 0 row value which overwrites or is overwritten by the A1, B1 entries in the headers.
Fixing these issues would give something like this:
import xlsxwriter
workbook = xlsxwriter.Workbook('S_details.xlsx')
sheet = workbook.add_worksheet()
# Generate some sample data.
student_result = []
for num in range(1, 11):
student_result.append([num] * 10)
# Make the columns wider so that the text is visible.
sheet.set_column('A:J', 20)
# Add some formatted headers.
bold = workbook.add_format({'bold': 1})
sheet.write('A1','Student_ID',bold)
sheet.write('B1','Student_Link',bold)
sheet.write('C1','Student_Name',bold)
sheet.write('D1','Student_Qualification',bold)
sheet.write('E1','Student_Address',bold)
sheet.write('F1','Student_City',bold)
sheet.write('G1','Student_State',bold)
sheet.write('H1','Student_Country',bold)
sheet.write('I1','Student_Stream',bold)
sheet.write('J1','Student_Gender',bold)
# Write the data.
for row_num, row_data in enumerate(student_result):
for col_num, col_data in enumerate(row_data):
sheet.write(row_num + 1, col_num, col_data)
workbook.close()

You preset rownum and colnum, but you're not using them in the write statement. How about:
sheet.write(rownum,colnum,col)
Also you probably don't want to advance rownum in the col for loop, so:
for a in student_result:
for r, row in enumerate(student_result):
for c, col in enumerate(row):
bold = workbook.add_format({'bold': 1})
sheet.write('A1','Student_ID',bold)
sheet.write('B1','Student_Link',bold)
sheet.write('C1','Student_Name',bold)
sheet.write('D1','Student_Qualification',bold)
sheet.write('E1','Student_Address',bold)
sheet.write('F1','Student_City',bold)
sheet.write('G1','Student_State',bold)
sheet.write('H1','Student_Country',bold)
sheet.write('I1','Student_Stream',bold)
sheet.write('J1','Student_Gender',bold)
sheet.write(rownum,colnum,col)
colnum = colnum + 1
rownum += 1

Related

Python Sqlite and Excel output with headers and Row ID

Please help to find correct solution from "simple to customize in future" point of view.
I have SQLite table and very big select. After this select I got 5 column and any rows.
I want to export this data to Special Excel file and Special Sheet. But not just export, I want add row = 0 with Headers of table. For example: header = [('Place', 'Players', 'Score', 'Delta', 'Game')].
For each row from SQLite I need add index to Place column from 1 to XXX.
Headers should be simple configure in future.
I try to directly import data from sqlite to excel, but in this case header not added. (here Players_Last_Day_Stat - sql select)
from xlsxwriter.workbook import Workbook
workbook = Workbook('Total_Stat.xlsx')
conn = create_connection()
c=conn.cursor()
worksheet = workbook.add_worksheet('Last-Day')
mysel=c.execute(Players_Last_Day_Stat)
for i, row in enumerate(mysel):
for j, value in enumerate(row):
if isinstance(value, float):
value = int(value)
worksheet.write(i, j, value)
But result like this
I expect this result finally:
Also, hot to change some cell bolt from python?
Thank you.
You're close. To make an index for your table, you can use worksheet.write_column. Here is what you can do to implement that (based on your code) and to shift the table (one column to the right and one row below) :
from xlsxwriter.workbook import Workbook
workbook = Workbook('Total_Stat.xlsx')
worksheet = workbook.add_worksheet('Last-Day')
conn = create_connection()
c = conn.cursor()
mysel = c.execute(Players_Last_Day_Stat)
header = ['Place', 'Players', 'Score', 'Delta', 'Game']
for idx, col in enumerate(header):
worksheet.write(0, idx, col) # <- write the column name one time in a row
#this was untouched
for i, row in enumerate(mysel):
for j, value in enumerate(row):
if isinstance(value, float):
value = int(value)
worksheet.write(i+1, j+1, value)
worksheet.write_column(1, 0, [i for i in range(1, len(c.execute(Players_Total_Stat).fetchall()) + 1)]) # make an index
#here, we make both 1st column/row bold
bold_fmt = workbook.add_format({'bold': True})
worksheet.set_row(0, None, bold_fmt)
worksheet.set_column(0, 0, None, bold_fmt)

Not able to export data into Excel using xlsxwriter library

I'm trying to iterate a For Loop such that the elements in the two lists get exported to excel in columns A and B. However, whenever I run the code it only displays a single number in column B row 1 (B1).
The entire code is too long so I'm attaching just a snippet of the code where I am stuck.
This is what I'm getting in my excel file when I run the code
#Exporting data to Excel
workbook = xlsxwriter.Workbook('efficient_front.xlsx')
worksheet = workbook.add_worksheet()
i = 1
if company == first_company:
for perc_return in returns:
worksheet.write('A' + str(i) , perc_return)
i =+ 1
else:
for perc_return in returns:
worksheet.write('B' + str(i), perc_return)
i =+ 1
workbook.close()
consider the given lists => prod_codes, ID_codes. The below code will write each list as a column in an excel sheet. The parameters of worksheet.write() are as shown below
worksheet.write(row_number,column_number,value_to_be_written)
prod_codes = [1001,1002,1003,1004,1005,1006]
ID_codes = [123,345,567,789,908,345]
with xlsxwriter.Workbook('PATH for XLSX to be created') as workbook:
worksheet = workbook.add_worksheet("NAME_ME")
for index,value in enumerate(ID_codes):
worksheet.write(index,0,value)
for index,value in enumerate(prod_codes):
worksheet.write(index,1,value)
Please go through the official documentation, it's clear how to perform what you need to perform. https://xlsxwriter.readthedocs.io/working_with_data.html
You have a silent syntax error in your code with i =+ 1 instead of i += 1. The code translates to i = +1 which is equivalent to i = 1 so it doesn't iterate.
Here is an alternative way to structure you code with enumerate() and the (row, col) syntax of worksheet.write():
import xlsxwriter
workbook = xlsxwriter.Workbook('efficient_front.xlsx')
worksheet = workbook.add_worksheet()
returns = [1, 2, 3, 4, 5]
company = True
first_company = False
if company == first_company:
col_num = 0
else:
col_num = 1
for row_num, perc_return in enumerate(returns):
worksheet.write(row_num, col_num, perc_return)
workbook.close()
Output:

Search specific text(text pattern) in excel and copy all resulting rows to another sheet in same workbook using openpyxl

I have an excel file with multiple sheets, 3rd column(contains around 500 rows) of sheet3 contains various names. I want to search column 3 for specific text and if it matches then copy the whole row along with the header row to new sheet within same excel.
Issue with "name column" is that most of the text refer to same item but naming convention is different, so:
अपर तहसीलदार,
नायाब तहसीलदार,
नायब तहसीलदार,
अतिरिक्त तहसीलदार
refers to same item but written differently, so for that I have to search for all variants.
I have no prior Python or openpyxl background so what I've got so far is:
import openpyxl
wb = openpyxl.load_workbook(r'C:/Users/Anas/Downloads/rcmspy.xlsx')
#active worksheet data
ws = wb.active
def wordfinder(searchString):
for i in range(1, ws.max_row + 1):
for j in range(1, ws.max_column + 1):
if searchString == ws.cell(i,j).value:
print("found")
print(ws.cell(i,j))
wordfinder("अपर तहसीलदार")
It is not showing any error but don't print anything either.
The excel sheet looks something like this:
I'm not certain, but I would suggest something along the lines of:
variants = {'alpha','alfa','elfa'}
data = []
rowCount = 0
for row in ws.values:
//each row is an array of cells
if rowCount == 0:
//header row
data.append(row)
elif row[2] in variants:
data.append(row)
rowCount += 1
wsNew = wb.create_sheet('Variations')
for line in data:
wsNew.append(line)
wb.save('newWorkbook.xlsx')

Writing new excel file without data from previous workbook

I am trying to write a programm to compare strings from a fixed matrix to 2 specific columns from an excel file. So far, I am first trying to achieve that a comparison with a match in row takes place. So far, the comparison of one string from the matrix is successful.
import openpyxl as xl
from IDM import idm_matrix
wb = xl.load_workbook('Auswertung_C33.xlsx')
sheet = wb['TriCad_Format']
for row in range(2, sheet.max_row + 1):
cell = sheet.cell(row, 8)
if idm_matrix[0][0] in cell.value:
sheet.cell(row=2, column=1).value = cell.value
wb.save('Auswertung.xlsx')
Question: How can I achieve that the matching values are saved in a new file WITHOUT the loaded workbook above?
For further help with this project I will get back to you as soon as I am facing more difficulties with the matrix comparison.
Thanks for your help.
Regards, Alex
You will need to create a new workbook to save your answer (comparison result). something like below. Hope this is helpful.
import openpyxl as xl
from IDM import idm_matrix
wb = xl.load_workbook('Auswertung_C33.xlsx')
result_wb = xl.Workbook() #workbook to save your result.
result_sheet = result_wb.active #get the active sheet to save your result.
sheet = wb['TriCad_Format']
for row in range(2, sheet.max_row + 1):
row_list = []
for col in range(1, sheet.max_col+1):
cell = sheet.cell(row, col)
row_list.append(cell)
#adjust row,col offset to match your matrix index below, e.g. row-2, col-1. you might need another loop to loop through your matrix.
if idm_matrix[i][j] in row_list:
result_sheet.append(row_list)
result_wb.save('Auswertung.xlsx') #save the result workbook
#henjiFire: Thats how the code looks like right now:
for row in range(2, sheet.max_row + 1):
row_list = []
for col in range(1, sheet.max_column + 1):
cell = sheet.cell(row, col)
row_list.append(cell.value)
# adjust row,col offset to match your matrix index below, e.g. row-2, col-1. you might need another loop to loop through your matrix.
if idm_matrix[0][0] in row_list:
if row_list[14] is not None and idm_matrix[0][1] in row_list[14]:
result_sheet.append(row_list)

How to copy data from one sheet to another while skipping empty cells - Python and Openpyxl

I'm using python to format an Excel spreadsheet. I need to copy data from Column L in Sheet #1, "Main", and paste it into Column A in Sheet #2, "Data". I've gotten this working, but I also want to skip empty cells, which occur randomly in Sheet #1, and here I ran intro trouble.
I tried:
for i in range(2, 50):
for j in range(12, 13):
if cell.value != None:
data.cell(row=i, column=j-11).value = main.cell(row=i, column=j).value
However I get the error message "NameError: name 'value' is not defined"
Any ideas?
This is the code we got working (see the comments for the back and forth):
import os
import openpyxl
wb = openpyxl.load_workbook('/Users/path/.xlsx')
main = wb['Sheet1']
wb.create_sheet(title='Formatted Data')
data = wb['Formatted Data']
for i in range(2, 50):
for j in range(12, 13):
if main.cell(i,j).value != None:
data.cell(data.max_row+1, column=j-11).value = main.cell(row=i, column=j).value
Wherever possible you should avoid using your own counters and let openpyxl do the work for you. For a new worksheet this is pretty easy.
empty_row = [None] * 11
for row in main.iter_rows(min_col=12, max_col=2, min_row=2, values_only=True):
if row[0] != None:
data.append(empty_row + row]

Categories