Not able to export data into Excel using xlsxwriter library - python

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:

Related

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)

Get excel cell address in Python 2 using openpyxl

I have the following scenario:
Read the excel file of column A for all active rows
Run if else statement: if column A == 'registered', do nothing and just read the next row. Else, get cell address (not value) and do some actions
For example, here is my "sample.xlsx":
[row 1] A1=registered B1=user1
[row 2] A2=registered B2=user2
[row 3] A3=null B3=user3
my code should ignore row1 and row2 and proceed with row 3. In row 3, I should get "A3" and do some actions.
from openpyxl import load_workbook
book = load_workbook(filename='sample.xlsx')
sheet = book.active
first_column = sheet['A']
for x in xrange(len(first_column)):
status = first_column[x].value
if status == 'registered':
#enter code to just proceed with the next row
else:
#enter code to get the column cell range and do some actions
book.save('sample.xlsx')
I got stucked with the scenario 2, I am a newbie in python, I have a code in java using FileScanner but I need it in Python 2. Appreciate your help.
UPDATE:
I already have an answer below but just wondering if there are any other method??
Have a look
import openpyxl
wb = openpyxl.load_workbook('sample.xlsx')
ws = wb.active
for row in ws.iter_rows('A{}:A{}'.format(ws.min_row,ws.max_row)):
for cell in row:
if cell.value != "registered":
print cell.column
print cell.row
## further processing here #######
No need to write logic for explicit row iteration this for loop will take care of that.
Hope it will help you :)
so far, I have tried these. is there any other method?
from openpyxl import load_workbook
book = load_workbook(filename='sample.xlsx')
sheet = book.active
first_column = sheet['A']
checker = 'false'
for x in xrange(len(first_column)):
status = first_column[x].value
xaddress = first_column[x].column
yaddress = first_column[x].row
celladdress = xaddress+str(yaddress)
if status != 'registered':
print celladdress
checker == 'true'
break
if checker == 'true':
#... my actions here ....
Here is my "sample.xlsx":
[row 1] A1=registered B1=user1
[row 2] A2=registered B2=user2
[row 3] A3=null B3=user3
Output: A3
It always helps to read the documentation
for cell in ws['A']:
pass

How to avoid overwriting of cell in excel using 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

data validation range Django and xlsxwriter

I have been using Django and xlsxwriter on a project that I am working on. I want to use data_validation in Sheet1 to pull in the lists that I have printed in Sheet2. I get the lists to print, but am not seeing the data_validation in Sheet1 when I open the file. Any insight on what I am doing incorrectly is much appreciated!
wb = xlsxwriter.Workbook(TestCass)
sh_1 = wb.add_worksheet()
sh_2 = wb.add_worksheet()
col = 15
head_col = 0
for header in headers:
sh_1.write(0,head_col,header)
sh_2.write(0,head_col,header)
list_row = 1
list = listFunction(headerToModelDic[header])
for entry in list:
sh_2.write(list_row,col,entry)
list_row += 1
sh_1.data_validation(1,col,50,col,{'validate':'list','source':'=Sheet2!$A2:$A9'})
col += 1
wb.close()
Note: The reason I am not pulling the list directly from the site is because it is too long (longer than 256 characters). Secondly, I ultimately would like the source range in the data validation to take in variables from sheet2, however I cannot get sheet 1 to have any sort of data validation as is so I figured I would start with the absolute values.
It looks like the data ranges are wrong in the example. It appears that you are writing out the list data in a column but the data validation refers to a row of data.
Maybe in your full example there is data in that row but in the example above there isn't.
I've modified your example slightly to a non-Django example with some sample data. I've also changed the data validation range to match the written data range:
import xlsxwriter
wb = xlsxwriter.Workbook('test.xlsx')
sh_1 = wb.add_worksheet()
sh_2 = wb.add_worksheet()
col = 15
head_col = 0
headers = ['Header 1']
for header in headers:
sh_1.write(0,head_col,header)
sh_2.write(0,head_col,header)
list_row = 1
list = [1, 2, 3, 4, 5]
for entry in list:
sh_2.write(list_row,col,entry)
list_row += 1
sh_1.data_validation(1,col,50,col,
{'validate':'list','source':'=Sheet2!$P2:$P6'})
col += 1
wb.close()
And here is the output:

Categories