Python Forum
How can I copy and paste data from text file into an Excel sheet?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How can I copy and paste data from text file into an Excel sheet?
#1
I am trying to automate a process that basically reads in values from text files into certain excel cells. I have a template in excel that will read data from various sheets under certain names. For example, the template will read in data from "Video scores". Video scores is a .txt file that I copy and paste into excel. There are 5 different text files used in each project so it gets tedious after a while and when there are a lot of projects to complete.

How can I import or copy and paste these .txt files into excel to a specified sheet? I have been using openpyxl for the other parts of this project, but I am open to using another library if it can't be done with openpxl.

I've also tried opening and reading a file, but I couldn't figure out how to do what I want with that either. I have found a list of all the files I need, its just a matter of getting them into excel.

Here is a sample of the .txt file: https://pastebin.com/FUvH6TbM

One thing I have tried to do is user pyperclip to open and copy the text file. I have that saved to a variable, so if I could somehow open excel and hit "paste" it would format fine.

If I were to manually copy all of the .txt file and paste it directly into excel, it would format absolutely perfectly. I just dont know how I can do this with Python.

Thanks in advance for anyone who helps.
Reply
#2
Is this the original file?
if so you have a problem, The file you posted to pastebin is space delimited, and it looks like a random number of spaces separates fields.

Is this the way the original file is, or are there tabs (or some other delimiter)?

if tabs (or other unique delimiter) you can treat the file as csv, import directly into excel
Reply
#3
(Mar-18-2020, 10:53 PM)Larz60+ Wrote: Is this the original file?
if so you have a problem, The file you posted to pastebin is space delimited, and it looks like a random number of spaces separates fields.

Is this the way the original file is, or are there tabs (or some other delimiter)?

if tabs (or other unique delimiter) you can treat the file as csv, import directly into excel

Hey thanks for the reply. For whatever reason its formatted weird in pastebin. The .txt file is formatted correctly and when I manually copy and paste it into excel, it also is pasted in correctly.

I tried converting it to a .csv and importing it that way but it didnt work right. How can I do this?
Reply
#4
the question is about the delimiter. what is it?

just attach a portion of the text file here.
instead of replying directly below post, choose New Reply button
from there you can add attachment.
Reply
#5
Sorry, im not sure how to attach a file. Here is the .txt file : https://file.io/eDaZIF
Reply
#6
bad link
if you read the rest of my post, I explained how to attach a file
Reply
#7
Hey Larz,

I did read your reply, im not sure if I am just being stupid or blind, but I cant find it Sad.

I can see link, image and code inserts, but not for a file an attachment. Here is what i see [Image: 6zBPWo8]

My apologies, its probably staring at me dead in the face! Anyways, i've uploaded that file to the cloud if you wanted to try and download it on your own computer here.

Sorry again! Hope that link is a better one and thanks a million for your help.
Reply
#8
use New Reply button when responding
it has attachment button or
correct your file link so can download
Reply
#9
@Larz - they are using New replay

@IrishOrigi, note that currently you don't have permission to post attachments.

https://python-forum.io/misc.php?action=help&hid=31
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#10
Ahh, that would explain it!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Why is the copy method name in python list copy and not `__copy__`? YouHoGeon 2 277 Apr-04-2024, 01:18 AM
Last Post: YouHoGeon
  Python openyxl not updating Excel file MrBean12 1 337 Mar-03-2024, 12:16 AM
Last Post: MrBean12
  Copy Paste excel files based on the first letters of the file name Viento 2 442 Feb-07-2024, 12:24 PM
Last Post: Viento
  What script to paste folders thenewcoder 1 675 Nov-29-2023, 09:40 AM
Last Post: Pedroski55
  Search Excel File with a list of values huzzug 4 1,249 Nov-03-2023, 05:35 PM
Last Post: huzzug
  Updating sharepoint excel file odd results cubangt 1 847 Nov-03-2023, 05:13 PM
Last Post: noisefloor
  How to copy work sheet data one workbook to other? sayyedkamran 2 701 Nov-03-2023, 09:10 AM
Last Post: Larz60+
  Python and pandas: Aggregate lines form Excel sheet Glyxbringer 12 1,877 Oct-31-2023, 10:21 AM
Last Post: Pedroski55
  Copy data from Excel and paste into Discord (Midjourney) Joe_Wright 4 2,060 Jun-06-2023, 05:49 PM
Last Post: rajeshgk
  Reading data from excel file –> process it >>then write to another excel output file Jennifer_Jone 0 1,112 Mar-14-2023, 07:59 PM
Last Post: Jennifer_Jone

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020