Soo
Soo

Reputation: 35

I would like to put a text file in existing excel file on specific cell using python

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?

Upvotes: 1

Views: 903

Answers (1)

Asa Kinory
Asa Kinory

Reputation: 51

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)

Upvotes: 2

Related Questions