1

I would like to put text file(calculation log - not not in tabular form) in existing excel file on specific cell.

enter image description here

import pandas as pd

log=open('path')

writer = pd.ExcelWriter('existing excel file', engine='xlsxwriter')

log.to_excel(writer , sheet_name='Sheet1', startcol=57,startrow=1)

writer.save()

I put other dataframes like this. However, this txt file cannot be made into a dataframe.

I want the txt file not to go into one cell, but like when I did control c + control v for the whole thing.

What should I do?

Soo
  • 35
  • 4

1 Answers1

2

You can use pandas to do that too. First, get your log with a context manager

with open('example text.txt', 'r') as file:
    log = file.read()

Get your excel file as a dataframe with no headers (column names will be the column numbers)

df = pd.read_excel('example excel.xlsx', header = None)

Put your text where you need it using column number and row number

df.at[row_number,column_number] = log

Then, if you need to, rewrite the excel file

df.to_excel('example excel.xlsx', index = False)
Asa Kinory
  • 51
  • 5
  • I get a new error message. df = pd.read_excel('example excel.xlsx', header = None) [BadZipFile: File is not a zip file] – Soo Dec 21 '21 at 17:04
  • This looks like a separate issue unfortunately. You can see more details here: https://stackoverflow.com/questions/57817758/badzipfile-file-is-not-a-zip-file-error-popped-up-all-of-a-sudden. Bottom line is: if you can, use a csv. Otherwise make sure what the file type is (xlsx, xls) and change the engine accordingly. – Asa Kinory Dec 22 '21 at 05:31