Python Forum
How to copy a .csv worksheet into a .xlsx file without the number values turning into
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to copy a .csv worksheet into a .xlsx file without the number values turning into
#7
Update: I figured it out

import sys
import openpyxl
import csv
import glob
from openpyxl.utils import get_column_letter

def write_to_target(target, directory, sheet_name):
    book = openpyxl.load_workbook(target)
    ws = book.get_sheet_by_name(sheet_name)
    f = open(directory)
    reader = csv.reader(f)
    for row_index, row in enumerate(reader):
        for column_index, cell in enumerate(row):
            column_letter = get_column_letter((column_index + 1))
            s = cell
            try:
                s=float(s)
            except ValueError:
                pass

            ws.cell('%s%s'%(column_letter, (row_index + 1))).value = s
Just replace target with the .xlsx file and directory with the .csv file

Thanks to everyone who've responded to this post
Reply


Messages In This Thread
RE: How to copy a .csv worksheet into a .xlsx file without the number values turning into - by YoshikageKira - Mar-28-2020, 09:38 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Errors if an xlsx file has blank rows in the beginning…. tester_V 1 862 Aug-28-2023, 06:22 PM
Last Post: deanhystad
  Export dataframe to xlsx - Error "zipfile.BadZipFile: File is not a zip file" Baggio 10 62,589 Mar-12-2021, 01:02 PM
Last Post: buran
  XLSX file with multiple sheets to josn file ovidius 2 2,264 Apr-05-2020, 09:22 AM
Last Post: ovidius
  spread values of dataset equally over fixed number of bins moose_man 3 2,539 Oct-30-2019, 07:41 PM
Last Post: ichabod801
  Import Excel File that Starts with Number kiki1113 1 3,343 Dec-20-2018, 07:13 PM
Last Post: Larz60+
  copy one column from csv file and paste into xls file kprogrammer 0 4,398 Nov-03-2018, 04:03 PM
Last Post: kprogrammer
  Copy raw data in excel to another new excel file keerthiprashanth 5 3,936 Oct-20-2018, 10:13 AM
Last Post: volcano63
  Searching a .txt file for a specific number and extracting the corresponding data nrozman 3 3,251 Jul-27-2018, 02:07 PM
Last Post: nrozman
  Updating the Pandas dataframe to existing excel workbook in existing worksheet. sanmaya 1 9,794 Jul-01-2018, 06:23 PM
Last Post: volcano63
  Write data into existing Excel (xlsx) file with multiple sheets BNB 1 15,390 Jun-01-2017, 04:22 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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