1. Táirgí
  2.   Scarbhileog
  3.   Python
  4.   XlsxWriter
 
  

Foinse Oscailte Python API le haghaidh Google Sheets

Scríobh Comhaid Scarbhileog Excel XML trí Leabharlann Python Foinse Oscailte.

Is API python foinse oscailte é XlsxWriter chun comhaid a scríobh i bhformáid comhaid Excel 2007+ XLSX. Ag baint úsáide as an API is féidir leat téacs, foirmlí, uimhreacha, agus hipearnasc a scríobh isteach i mbileoga oibre iolracha. Ina theannta sin, ceadaíonn an API cairteacha a chur isteach, cealla cumaisc, cealla formáide, scagairí a chur i bhfeidhm, sonraí bailíochtaithe, íomhánna PNG/JPEG/BMP/WMF/EMF a chur isteach, teaghráin shaibhir ilfhormáideacha a úsáid, agus go leor eile.

Éilíonn XlsxWriter go soláthraíonn sé níos mó Gnéithe Excel ná aon cheann de na modúil python malartacha. Soláthraíonn an API ráta ard cruinnis agus comhaid Excel nua á gcruthú, i bhformhór na gcásanna tá na comhaid a tháirgtear trí XlsxWriter 100% comhionann le comhaid arna dtáirgeadh ag Excel.

Previous Next

Tús a chur le XlsxWriter

Ní mór duit Python 2.7 nó níos airde a bheith suiteáilte ar do chóras chun XlsxWriter a rith go réidh. An bealach molta a shuiteáil trí PIP. Bain úsáid as an ordú seo a leanas le do thoil.

Suiteáil XlsxWriter trí Ordú PIP

pip install XlsxWriter

Cruthaigh Scarbhileog trí Leabharlann Python

Ceadaíonn XlsxWriter API Scarbhileoga Microsoft a chruthú ag baint úsáide as Python agus an modúl XlsxWriter. Ligeann sé do ríomhchláraitheoirí bogearraí scarbhileog bhán a chruthú ag baint úsáide as modh XlsxWriter.Workbook(). Is féidir leat bileoga oibre a chur le do leabhar oibre trí úsáid a bhaint as modh workbook.add_worksheet(). Tar éis na bileoga oibre a chur leis, ceadaíonn an API raon leathan gnéithe saor in aisce a bheith ag obair le bileoga excel ag baint úsáide as Python.

Cruthaigh & Athraigh Scarbhileog trí Leabharlann Python<

import xlsxwriter
# Create an new Excel file and add a worksheet.
workbook = xlsxwriter.Workbook('demo.xlsx')
worksheet = workbook.add_worksheet()
# Widen the first column to make the text clearer.
worksheet.set_column('A:A', 20)
# Add a bold format to use to highlight cells.
bold = workbook.add_format({'bold': True})
# Write some simple text.
worksheet.write('A1', 'Hello')
# Text with formatting.
worksheet.write('A2', 'World', bold)
# Write some numbers, with row/column notation.
worksheet.write(2, 0, 123)
worksheet.write(3, 0, 123.456)
workbook.close()

Cuir Cairteacha leis in XLSX ag baint úsáide as Python

Ceadaíonn leabharlann scarbhileog Foinse Oscailte XlsxWriter d’fhorbróirí bogearraí cairteacha a chur leis i bhformáid comhaid XLSX ag baint úsáide as ach cúpla líne de chód. Tar éis duit do bhileog oibre nua a chruthú in Excel, is féidir leat cairt a chur leis trí mhodh workbook.add_chart() a úsáid. Ag baint úsáide as an Python API, is féidir leat cairteacha achair, barrachairteacha, cairteacha colún, cairteacha líne, píchairteacha, cairteacha donut, cairteacha scaipthe, cairteacha stoic, agus cairteacha radair a chur leis saor in aisce.

Cuir Barrachairt le Scarbhileog XLSX trí Leabharlann Python<

import xlsxwriter
workbook = xlsxwriter.Workbook('chart_bar.xlsx')
worksheet = workbook.add_worksheet()
bold = workbook.add_format({'bold': 1})
# Add the worksheet data that the charts will refer to.
headings = ['Number', 'Batch 1', 'Batch 2']
data = [
    [2, 3, 4, 5, 6, 7],
    [10, 40, 50, 20, 10, 50],
    [30, 60, 70, 50, 40, 30],
]
worksheet.write_row('A1', headings, bold)
worksheet.write_column('A2', data[0])
worksheet.write_column('B2', data[1])
worksheet.write_column('C2', data[2])
# Create a new bar chart.
chart1 = workbook.add_chart({'type': 'bar'})
# Configure the first series.
chart1.add_series({
    'name':       '=Sheet1!$B$1',
    'categories': '=Sheet1!$A$2:$A$7',
    'values':     '=Sheet1!$B$2:$B$7',
})

Oibriú le Foirmlí Excel ag baint úsáide as Python

Tugann leabharlann XlsxWriter an cumas d'fhorbróirí foirmlí a scríobh laistigh de bhformáid comhaid Microsoft Excel go ríomhchláraithe. Is féidir leat foirmle a chur le do chomhad ach úsáid a bhaint as modh worksheet.write_forumula(). Stórálann Excel foirmlí i bhformáid leagan Béarla SAM, mar sin ní mór na foirmlí a bheith i bhformáid Bhéarla SAM.

Cuir Foirmle Eagar leis trí Leabharlann Python<

import xlsxwriter
# Create a new workbook and add a worksheet
workbook = xlsxwriter.Workbook('array_formula.xlsx')
worksheet = workbook.add_worksheet()
# Write some test data.
worksheet.write('B1', 500)
worksheet.write('B2', 10)
worksheet.write('B5', 1)
worksheet.write('B6', 2)
worksheet.write('B7', 3)
worksheet.write('C1', 300)
worksheet.write('C2', 15)
worksheet.write('C5', 20234)
worksheet.write('C6', 21003)
worksheet.write('C7', 10000)
# Write an array formula that returns a single value
worksheet.write_formula('A1', '{=SUM(B1:C1*B2:C2)}')
# Same as above but more verbose.
worksheet.write_array_formula('A2:A2', '{=SUM(B1:C1*B2:C2)}')
# Write an array formula that returns a range of values
worksheet.write_array_formula('A5:A7', '{=TREND(C5:C7,B5:B7)}')
workbook.close()
 Gaeilge