How to write/update data into cells of existing XLSX workbook using xlsxwriter in python

后端 未结 5 807
说谎
说谎 2020-12-01 04:47

I am able to write into new xlsx workbook using

import xlsxwriter  
def write_column(csvlist):
    workbook = xlsxwriter.Workbook(\"filename.xlsx\",{\'strin         


        
相关标签:
5条回答
  • 2020-12-01 05:03

    Quote from xlsxwriter module documentation:

    This module cannot be used to modify or write to an existing Excel XLSX file.

    If you want to modify existing xlsx workbook, consider using openpyxl module.

    See also:

    • Modify an existing Excel file using Openpyxl in Python
    • Use openpyxl to edit a Excel2007 file (.xlsx) without changing its own styles?
    0 讨论(0)
  • 2020-12-01 05:08

    If you have issue with writing into an existing xls file because it is already created you need to put checking part like below:

    PATH='filename.xlsx'
    if os.path.isfile(PATH):
        print "File exists and will be overwrite NOW"
    else:
        print "The file is missing, new one is created"
    

    ... and here part with the data you want to add

    0 讨论(0)
  • 2020-12-01 05:09

    you can use this code to open (test.xlsx) file and modify A1 cell and then save it with a new name

    import openpyxl
    xfile = openpyxl.load_workbook('test.xlsx')
    
    sheet = xfile.get_sheet_by_name('Sheet1')
    sheet['A1'] = 'hello world'
    xfile.save('text2.xlsx')
    
    0 讨论(0)
  • 2020-12-01 05:14

    Note that openpyxl does not have a large toolbox for manipulating and editing images. Xlsxwriter has methods for images, but on the other hand cannot import existing worksheets...

    I have found that this works for rows... I'm sure there's a way to do it for columns...

    import openpyxl
    
    oxl = openpyxl.load_workbook('File Loction Here')
    xl = oxl.['SheetName']
    
    x=0
    col = "A"
    row = x
    
    while (row <= 100):
        y = str(row)
        cell = col + row
        xl[cell] = x
        row = row + 1
        x = x + 1
    
    0 讨论(0)
  • 2020-12-01 05:27

    You can do by xlwings as well

    import xlwings as xw
    for book in xlwings.books:
        print(book)
    
    0 讨论(0)
提交回复
热议问题