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()
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…
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. …
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…
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…
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…
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 …
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 =…
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…
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…