1

Whenever there is a matching keyword, my code prints the next line. I'm trying to export the entire print output to an excel file automatically. The code works perfectly for printing the output in the IDE terminal, but I can't seem to export it to excel. The for loop is giving me trouble. Which ever command I try, ends up giving an error. Any pointers in the right direction are much appreciated.

Sample text to try the code on:

Hi, please enter name

Ricky

can you please enter last name

Martin

Please enter the full name

Ricky Martin

Output:

Ricky

Martin

Ricky Martin

Question

How to get these outputs exported to a column in Excel?

import xlsxwriter

excelFile = xlsxwriter.Workbook ("goal.xlsx")
workSheet = excelFile.add_worksheet ()

with open("filepath.txt", 'r') as f:

   for line in f:
        if "enter" in line:
            result = print(next (f, ''))
            workSheet.write('A1', result)
            

excelFile.close()
6
  • You have not shown how you want your output data to look like in Excel. Commented Jan 7, 2021 at 21:31
  • You don't have any code in your example writing to the workSheet object. Take a look here - xlsxwriter.readthedocs.io/worksheet.html Commented Jan 7, 2021 at 22:33
  • @JustinEzequiel thank you for commenting. The output data should go to any column in excel. For example it can go in column A and look like this: Ricky Martin Ricky Martin. I hope I'm making sense. I tried to add this line of code in my question above, but it's not working. Commented Jan 8, 2021 at 7:26
  • @RiskyMick thank you for commenting. Just added that part of code in the original question. but it doesn't work. I have gone through the documentation link but can't figure out the issue. Commented Jan 8, 2021 at 7:31
  • The simplest way to achieve what you want to do is to just write to a csv file. Import this csv file in excel and you are done. CSV is a widely supported filetype for python and super easy to implement. It does not change anything with respect to immediately formatting it to an excel file as you have to 'open' the excel file as well as 'import' the csv file in excel. Commented Jan 8, 2021 at 8:04

1 Answer 1

2

I can see you have a print statement for result. So it returns None. Could be the issue.

I haven't worked with xlsxwriter much. But, I have a working pandas example which is fairly simple and works with list so that you can get a pointer.

import pandas as pd
lstSrc=[1,2,3,4]
df=pd.DataFrame() 
df["TimeTook"] = lstSrc
writer = pd.ExcelWriter("result.xlsx",engine='xlsxwriter')
df.to_excel(writer,sheet_name='sheetname')
writer.save()

Referred xlsxwriter docs for sample.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.