Can I get a text file from Python and put it in the existing Excel file?

Asked 2 years ago, Updated 2 years ago, 39 views

Hi, how are you?

I calculated using Jupiter's laptop, made the calculated results into a data frame, and stored them in an Excel file. After that, I tried to put the method used for calculation and the log file with the calculation time in the excel sheet.

For general window work other than Python, you can open the txt file and drag the entire file to control c+v.

Since I'm trying to do it on Python, I can't find it even if I search for it. I don't know what to do.

First of all

This is what I want to be copied in this is what it looks like The code I tried is

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()

In the past, data frames in the form of a table were put in the form of an expression txt doesn't work properly because it's not tabular. What should I do?

python pandas

2022-09-20 11:39

1 Answers

to_excel is the method in the pandas dataframe.

I think we can do it like this.

import pandas as pd

with open("log.txt") as f:
    log_lines = f.readlines()

log_df = pd.DataFrame(log_lines)
with pd.ExcelWriter("logexcel.xlsx", engine="openpyxl", mode="a") as writer:

    log_df.to_excel(writer, sheet_name="Sheet3", startcol=2, startrow=2)
    writer.save()

When adding to the existing Excel file, the append mode must be set (mode="a"), and the append mode cannot be used when the engine is xlsxwriter.


2022-09-20 11:39

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.