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
#1
Hello there!

I'm having a problem with copying a .csv file into a .xlsx file. It does work because the information is there but the numbers do not register as numbers but as text values which causes the formulas in my .xlsx file to not work.

Keep in mind that I'm trying to copy my .csv sheet into a specific sheet in my .xlsx file, hence the sheet name. Here is my function:

import pandas as pd
import openpyxl

def write_to_target(xlsx_file, csv_file, sheet_name):
    book = openpyxl.load_workbook(xlsx)
    sheet_book = pd.read_csv(csv_file)

    with pd.ExcelWriter(xlsx_file, engine='openpyxl')  as writer:
        writer.book = book
        writer.sheets = dict((ws.title, ws) for ws in book.worksheets)
        sheet_book.to_excel(writer, sheet_name=sheet_name, startrow=0, startcol=0, engine = 'openpyxl')
Thanks in advance!
Reply


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

Possibly Related Threads…
Thread Author Replies Views Last Post
  Errors if an xlsx file has blank rows in the beginning…. tester_V 1 888 Aug-28-2023, 06:22 PM
Last Post: deanhystad
  Export dataframe to xlsx - Error "zipfile.BadZipFile: File is not a zip file" Baggio 10 63,083 Mar-12-2021, 01:02 PM
Last Post: buran
  XLSX file with multiple sheets to josn file ovidius 2 2,291 Apr-05-2020, 09:22 AM
Last Post: ovidius
  spread values of dataset equally over fixed number of bins moose_man 3 2,580 Oct-30-2019, 07:41 PM
Last Post: ichabod801
  Import Excel File that Starts with Number kiki1113 1 3,369 Dec-20-2018, 07:13 PM
Last Post: Larz60+
  copy one column from csv file and paste into xls file kprogrammer 0 4,424 Nov-03-2018, 04:03 PM
Last Post: kprogrammer
  Copy raw data in excel to another new excel file keerthiprashanth 5 3,976 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,294 Jul-27-2018, 02:07 PM
Last Post: nrozman
  Updating the Pandas dataframe to existing excel workbook in existing worksheet. sanmaya 1 9,822 Jul-01-2018, 06:23 PM
Last Post: volcano63
  Write data into existing Excel (xlsx) file with multiple sheets BNB 1 15,428 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