Copying text from file to specified Excel column [closed]

2024/11/16 19:42:07

I've got a text file, say, "A.txt" and an Excel file, say "B.xlsx". My goal is to copy all the text from A.txt to a specified column in B.xlsx using a python script. For example, let's say A.txt looks like:

Word1 5
Word2 Word3
6 Word4

I would then want to copy this into, say, column J in worksheet "Words", such that the contents of cell J1 are "Word1 5", the contents of J2 are "Word2 Word3", and so on...

So, to reformulate the question, how can I copy text from a specified text file and paste it into a specified Excel column, such that the content of each cell is equivalent to the content of each line (one-to-one relationship)?

Many thanks in advance for any help!

Answer

Use the win32com library to interface directly with microsoft excel, as you are working in excel:

import win32com.client#Read text file lines into list
f = open("A.txt")
text_contents = f.readlines()# Open excel and your workbook
col = 2 # column B
excel=win32com.client.Dispatch("Excel.Application")
excel.Visible=True # Note: set to false when scripting, only True for this example
wb=excel.Workbooks.Open('B.xlsx')
ws = wb.Worksheets('Sheet1')#Write text contents to column range
ws.Range(ws.Cells(col ,1),ws.Cells(col,len(text_contents))).Value = text_contents#Save the workbook and quit
wb.Close(True)
excel.Application.Quit() 
https://en.xdnf.cn/q/118893.html

Related Q&A

Name error: Variable not defined

Program calculates the shortest route from point, to line, then to second point. Also I need to say how long is from the start of the line, to where point crosses. My code so far: from math import sqrt…

Error while deploying flask app on apache

I have a file manage.py, import os from app import create_app app = create_app(os.getenv(FLASK_CONFIG) or default) if __name__ == __main__:app.run()manage.py is working fine when tested in debug mode. …

Selenium Python get_element by ID failing

Can someone help me understand why my code fails to find the element by ID. Code below:from selenium import webdriver driver=webdriver.Firefox() driver.get(https://app.waitwhile.com/checkin/lltest3/use…

Pipelining POST requests with python-requests

Assuming that I can verify that a bunch of POST requests are in fact logically independent, how can I set up HTTP pipelining using python-requests and force it to allow POST requests in the pipeline?D…

How to take a whole matrix as a input in Python?

I want to take a whole matrix as an input in Python and store it in a dataframe. Pandas can do it automatically with read_csv function but it requires a CSV file. I want to input/copy-paste a matrix di…

Cannot create environment in anaconda, update conda , install packages

CondaHTTPError: HTTP 000 CONNECTION FAILED for url https://repo.anaconda.com/pkgs/free/win-64/repodata.json.bz2 Elapsed: -An HTTP error occurred when trying to retrieve this URL. HTTP errors are often …

Inverted Triangle in Python-not running

I have to create a program to print an inverted triangle in python. When I was running it in Sublime Text 3 it did not run. By that, I mean that it did not even print a syntax error. def triangle():x =…

How to do Data profile to a table using pandas_profiling

When Im trying to do data profiling one sql server table by using pandas_profiling throwing an error like An attempt has been made to start a new process before thecurrent process has finished its boot…

Python replace line by index number

Is it possible in Python to replace the content of a line in a file by its index number?Would something like a line.replace to do this procedure?

print/list only 5 entries from OS.Walk in python

My Goal - To list only 5 entries when using OS walk. So far I have only been able to get a list of everything that I find using OS.Walk or only list one entry. (By using the return function) My code: i…