下面为大家分享一篇python写入已存在的excel数据实例,具有很好的参考价值,希望对大家有所帮助。一起过来看看吧
python可以使用xlrd读excel,使用xlwt写excel,但是如果要把数据写入已存在的excel,需要另外一个库xlutils配合使用.
大概思路:
1、用xlrd.open_workbook打开已有的xsl文件
注意添加参数formatting_info=True,得以保存之前数据的格式
2、然后用,from xlutils.copy import copy;,之后的copy去从打开的xlrd的Book变量中,拷贝出一份,成为新的xlwt的Workbook变量
3、然后对于xlwt的Workbook变量,就是正常的:
通过get_sheet去获得对应的sheet,拿到sheet变量后,就可以往sheet中,写入新的数据
4、写完新数据后,最终save保存
源码例子:
import xlrd import os from xlutils.copy import copy from xlwt import Style def writeExcel(row, col, str, styl=Style.default_style): rb = xlrd.open_workbook(file, formatting_info=True) wb = copy(rb) ws = wb.get_sheet(0) ws.write(row, col, str, styl) wb.save(file) style = xlwt.easyxf('font:height 240, color-index red, bold on;align: wrap on, vert centre, horiz center'); writeExcel(1, 1, 'hello world', style)
如果需要excel原格式,需要加参数
formatting_info=True
如果需要加excel样式,传入样式字符串给xlwt.easyxf即可
合并单元格:
ws.write_merge(top_row, bottom_row, left_column, right_column, string)
相关推荐:
Atas ialah kandungan terperinci python写入已存在的excel数据实例. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Slicingapythonlistisdoneusingthesyntaxlist [Mula: berhenti: langkah] .here'showitworks: 1) startistheindexofthefirstelementtoinclude.2) stopistheindexofthefirstelementToexclude.3)

NumpyallowsforvariousoperationsonArrays: 1) BasicarithmeticLikeaddition, penolakan, pendaraban, danDivision; 2) Pengerjaan AdvancedSuchasmatrixmultiplication; 3) Element-WiseOperationswithoutExplicitLoops;

Arraysinpython, terutamanya yang ada, adalah, penawaran yang ditawarkan.1) numpyarraysenableFandlingoflargedataSetsandClexPleperationsLikemovingAverages.2)

ListsSandnumpyAraySInpythonHavedifferMememoryFootPrints: listsaremoreflexibleButlessMememory-cekap, pemanmak

ToensurePythonscriptsbehaveCorrectlyCrossdevelopment, pementasan, dan produksi, usetheseStregies: 1) Environmentvariablesforsimplesettings, 2) ConfigurationFilesfilePlexSetups, dan3) Dynamicloadingforadaptability.EachMethodeFerPiReFiteReFiteShitsandReFitSandRiteFitSandRiteFitSandRiteFiteSandRiteReFitSandRiteReFitSandRiteFiteShiteSandReFiteShitsandReShitsAnfitsEts,

Sintaks asas untuk pengirim senarai python adalah senarai [Mula: Berhenti: Langkah]. 1. Start adalah indeks elemen pertama yang disertakan, 2.Stop adalah indeks elemen pertama yang dikecualikan, dan 3. Step menentukan saiz langkah antara elemen. Hirisan tidak hanya digunakan untuk mengekstrak data, tetapi juga untuk mengubah suai dan membalikkan senarai.

ListsOutPerFormAraySin: 1) DynamicsizingandFrequentInsertions/Deletions, 2) StoringHeterogeneousData, dan3) MemoryeficiencyForSparsedata, ButmayHaveslightPerformancecostSincertaor.

ToConvertapythonarraytoalist, usethelist () constructororageneratorexpression.1) importTheArrayModuleAndCreateeanArray.2) uselist (arr) atau [xforxinarr] toConvertittoalist, urusanPengerasiPormanceAndMemoryeficiencyForlargedatasets.


Alat AI Hot

Undresser.AI Undress
Apl berkuasa AI untuk mencipta foto bogel yang realistik

AI Clothes Remover
Alat AI dalam talian untuk mengeluarkan pakaian daripada foto.

Undress AI Tool
Gambar buka pakaian secara percuma

Clothoff.io
Penyingkiran pakaian AI

Video Face Swap
Tukar muka dalam mana-mana video dengan mudah menggunakan alat tukar muka AI percuma kami!

Artikel Panas

Alat panas

Versi Mac WebStorm
Alat pembangunan JavaScript yang berguna

SublimeText3 versi Inggeris
Disyorkan: Versi Win, menyokong gesaan kod!

EditPlus versi Cina retak
Saiz kecil, penyerlahan sintaks, tidak menyokong fungsi gesaan kod

ZendStudio 13.5.1 Mac
Persekitaran pembangunan bersepadu PHP yang berkuasa

Muat turun versi mac editor Atom
Editor sumber terbuka yang paling popular
