[{"data":1,"prerenderedAt":1091},["ShallowReactive",2],{"doc:\u002Fgetting-started-with-python-excel-automation\u002Fusing-python-in-excel\u002Fget-external-data-into-python-in-excel":3,"surround:\u002Fgetting-started-with-python-excel-automation\u002Fusing-python-in-excel\u002Fget-external-data-into-python-in-excel":1063,"ancestors:\u002Fgetting-started-with-python-excel-automation\u002Fusing-python-in-excel\u002Fget-external-data-into-python-in-excel":1072,"children:\u002Fgetting-started-with-python-excel-automation\u002Fusing-python-in-excel\u002Fget-external-data-into-python-in-excel":1077,"related:\u002Fgetting-started-with-python-excel-automation\u002Fusing-python-in-excel\u002Fget-external-data-into-python-in-excel":1078},{"id":4,"title":5,"body":6,"dateModified":1028,"datePublished":1028,"description":1029,"extension":1030,"faq":1031,"keyword":1040,"kind":1041,"meta":1042,"navigation":1051,"path":1052,"seo":1053,"slug":1056,"stem":1057,"topics":1058,"type":1061,"__hash__":1062},"docs\u002Fgetting-started-with-python-excel-automation\u002Fusing-python-in-excel\u002Fget-external-data-into-python-in-excel\u002Findex.md","Get External Data into Python in Excel with Power Query",{"type":7,"value":8,"toc":1014},"minimark",[9,32,183,188,191,195,220,229,236,240,246,266,379,382,386,393,441,444,489,506,510,517,684,691,695,698,807,810,814,825,879,886,890,923,927,939,943,949,953,959,965,971,977,981,1010],[10,11,12,13,17,18,21,22,25,26,31],"p",{},"The first thing people try in a ",[14,15,16],"code",{},"=PY()"," cell is ",[14,19,20],{},"pd.read_csv(\"C:\u002Fdata\u002Fsales.csv\")"," or ",[14,23,24],{},"requests.get(...)",", and both fail. Python in Excel runs in an isolated container in Microsoft's cloud: it has no view of your disk and no internet access, by design. Everything it analyses must already be in the workbook. The supported way to get it there is Power Query — Excel's built-in import engine, under Data → Get Data — which can pull from files, folders, databases and web sources, refresh on demand, and hand the result to Python by name. This guide, part of ",[27,28,30],"a",{"href":29},"\u002Fgetting-started-with-python-excel-automation\u002Fusing-python-in-excel\u002F","Using Python in Excel",", sets up that pipeline and explains the refresh behaviour that makes it reliable.",[33,34,42,43,42,47,42,51,42,59,42,69,42,80,42,87,42,92,42,99,42,104,42,109,42,116,42,121,42,124,42,128,42,131,42,135,42,142,42,147,42,150,42,154,42,157,42,162,42,169,42,174,42,177],"svg",{"viewBox":35,"role":36,"ariaLabelledBy":37,"xmlns":40,"style":41},"4 0 752 151","img",[38,39],"pq-flow-t","pq-flow-d","http:\u002F\u002Fwww.w3.org\u002F2000\u002Fsvg","width:100%;max-width:752px;height:auto;display:block;margin:1.5rem auto;font-family:Inter,ui-sans-serif,system-ui,sans-serif","\n  ",[44,45,46],"title",{"id":38},"External data reaching Python in Excel through Power Query",[48,49,50],"desc",{"id":39},"Power Query connects to an external source such as a CSV file, a database or a web API, shapes the data, and loads it as a table or a connection-only query in the workbook. A PY cell reads that result with xl and runs pandas on it in the cloud.",[52,53],"rect",{"x":54,"y":55,"width":56,"height":57,"fill":58},"4","0","752","151","#ffffff",[52,60],{"x":61,"y":62,"width":63,"height":64,"rx":65,"fill":66,"stroke":67,"style":68},"20.0","16.0","153.0","93.0","12","#e7ebef","var(--line,#cdd5e6)","stroke-width:2px",[70,71,73],"g",{"transform":72},"translate(84.5,29.0) scale(1.000)",[74,75],"path",{"d":76,"fill":77,"stroke":78,"style":79},"M5 6c0-1.7 3.1-3 7-3s7 1.3 7 3-3.1 3-7 3-7-1.3-7-3z M5 6v12c0 1.7 3.1 3 7 3s7-1.3 7-3V6 M5 12c0 1.7 3.1 3 7 3s7-1.3 7-3","none","var(--text,#172033)","stroke-width:1.80px;stroke-linecap:round;stroke-linejoin:round",[81,82,86],"text",{"x":83,"y":84,"style":85},"96.5","75.5","font-size:13.5px;font-weight:700;fill:var(--text,#172033);text-anchor:middle","External source",[81,88,91],{"x":83,"y":89,"style":90},"95.0","font-size:11.5px;font-weight:400;fill:var(--text,#172033);text-anchor:middle","CSV, SQL, web",[93,94],"line",{"x1":95,"y1":96,"x2":97,"y2":96,"stroke":98,"style":68},"178.0","62.5","200.0","var(--brand,#5b5cf0)",[100,101],"polygon",{"points":102,"fill":103},"204.0,62.5 195.0,67.5 195.0,57.5","#5b5cf0",[52,105],{"x":106,"y":62,"width":63,"height":64,"rx":65,"fill":107,"stroke":108,"style":68},"209.0","#fdefd8","var(--gold,#b4740a)",[70,110,112],{"transform":111},"translate(273.5,29.0) scale(1.000)",[74,113],{"d":114,"fill":77,"stroke":115,"style":79},"M3 5h18l-7 8v6l-4 2v-8z","var(--gold-ink,#7a4e06)",[81,117,120],{"x":118,"y":84,"style":119},"285.5","font-size:13.5px;font-weight:700;fill:var(--gold-ink,#7a4e06);text-anchor:middle","Power Query",[81,122,123],{"x":118,"y":89,"style":90},"import, filter, shape",[93,125],{"x1":126,"y1":96,"x2":127,"y2":96,"stroke":98,"style":68},"367.0","389.0",[100,129],{"points":130,"fill":103},"393.0,62.5 384.0,67.5 384.0,57.5",[52,132],{"x":133,"y":62,"width":63,"height":64,"rx":65,"fill":134,"stroke":98,"style":68},"398.0","#f0f4ff",[70,136,138],{"transform":137},"translate(462.5,29.0) scale(1.000)",[74,139],{"d":140,"fill":77,"stroke":141,"style":79},"M3 5h18v14H3z M3 10h18 M3 15h18 M9 5v14","var(--brand-strong,#4338ca)",[81,143,146],{"x":144,"y":84,"style":145},"474.5","font-size:13.5px;font-weight:700;fill:var(--brand-strong,#4338ca);text-anchor:middle","Table or query",[81,148,149],{"x":144,"y":89,"style":90},"inside the workbook",[93,151],{"x1":152,"y1":96,"x2":153,"y2":96,"stroke":98,"style":68},"556.0","578.0",[100,155],{"points":156,"fill":103},"582.0,62.5 573.0,67.5 573.0,57.5",[52,158],{"x":159,"y":62,"width":63,"height":64,"rx":65,"fill":160,"stroke":161,"style":68},"587.0","#d9f4f1","var(--teal,#0f9488)",[70,163,165],{"transform":164},"translate(651.5,29.0) scale(1.000)",[74,166],{"d":167,"fill":77,"stroke":168,"style":79},"M12 3c-4 0-4 1.6-4 3v2h4.2v1H6c-2 0-3 1.4-3 4s1 4 3 4h2v-2.6c0-1.6 1.3-2.9 2.9-2.9h4.2c1.3 0 2.4-1.1 2.4-2.4V6c0-1.6-1.8-3-5.5-3z M10 5.5h.01 M12 21c4 0 4-1.6 4-3v-2h-4.2v-1H18c2 0 3-1.4 3-4s-1-4-3-4h-2v2.6c0 1.6-1.3 2.9-2.9 2.9H8.9c-1.3 0-2.4 1.1-2.4 2.4V18c0 1.6 1.8 3 5.5 3z M14 18.5h.01","var(--teal-ink,#0b6157)",[81,170,173],{"x":171,"y":84,"style":172},"663.5","font-size:13.5px;font-weight:700;fill:var(--teal-ink,#0b6157);text-anchor:middle","=PY() cell",[81,175,176],{"x":171,"y":89,"style":90},"xl(\"Sales\") → pandas",[81,178,182],{"x":179,"y":180,"style":181},"380.0","131.0","font-size:12.5px;font-weight:400;fill:var(--muted,#5b6780);text-anchor:middle","Python never touches the source — Power Query brings the data in, Python reads it from the workbook",[184,185,187],"h2",{"id":186},"prerequisites","Prerequisites",[10,189,190],{},"You need Excel for Microsoft 365 with Python in Excel enabled for your plan, and permission to connect to the source from Power Query (file access, database credentials or an API key). No Python installation is involved; the libraries run in Microsoft's environment.",[184,192,194],{"id":193},"step-1-import-the-source-with-power-query","Step 1 — Import the source with Power Query",[10,196,197,198,202,203,207,208,211,212,215,216,219],{},"Choose ",[199,200,201],"strong",{},"Data → Get Data"," and the source type: ",[204,205,206],"em",{},"From File → From Text\u002FCSV"," for a single file, ",[204,209,210],{},"From File → From Folder"," for a folder of monthly exports, ",[204,213,214],{},"From Database"," for SQL Server and others, or ",[204,217,218],{},"From Other Sources → From Web"," for a URL. The Power Query editor opens with a preview. Do the cheap shaping here, before Python sees anything: remove columns you do not need, filter to the period you analyse, set data types. Power Query records each step in its own formula language, M; a typical import of a CSV looks like this in the Advanced Editor:",[221,222,227],"pre",{"className":223,"code":225,"language":81,"meta":226},[224],"language-text","let\n    Source = Csv.Document(File.Contents(\"C:\\Data\\sales_2026.csv\"), [Delimiter=\",\", Encoding=65001]),\n    Promoted = Table.PromoteHeaders(Source, [PromoteAllScalars=true]),\n    Typed = Table.TransformColumnTypes(Promoted, {{\"order_date\", type date}, {\"amount\", type number}}),\n    Recent = Table.SelectRows(Typed, each [order_date] >= #date(2026, 1, 1))\nin\n    Recent\n","",[14,228,225],{"__ignoreMap":226},[10,230,231,232,235],{},"You rarely need to write M by hand — each click in the editor adds a step — but reading it tells you exactly what arrives in the workbook. Name the query something meaningful, such as ",[14,233,234],{},"Sales",", because that name is what Python will use.",[184,237,239],{"id":238},"step-2-load-as-a-table-or-keep-it-as-a-connection","Step 2 — Load as a table, or keep it as a connection",[10,241,242,245],{},[199,243,244],{},"Close & Load To…"," offers two choices that matter here:",[247,248,249,260],"ul",{},[250,251,252,255,256,259],"li",{},[199,253,254],{},"Table"," puts the result on a sheet as an Excel table named after the query. People can see and filter it, and ",[14,257,258],{},"xl(\"Sales[#All]\", headers=True)"," reads it.",[250,261,262,265],{},[199,263,264],{},"Only Create Connection"," keeps the data in the workbook's data model of queries without writing it to a sheet. A PY cell can still read it by query name.",[33,267,42,272,42,275,42,278,42,281,42,286,42,292,42,298,42,304,42,310,42,317,42,320,42,325,42,329,42,336,42,338,42,344,42,349,42,353,42,357,42,362,42,365,42,370,42,373],{"viewBox":268,"role":36,"ariaLabelledBy":269,"xmlns":40,"style":41},"4 0 752 207",[270,271],"pq-load-t","pq-load-d",[44,273,274],{"id":270},"Loading a Power Query result as a table or as a connection only",[48,276,277],{"id":271},"Loading as a table places the data on a sheet where people can see it and xl reads the table, but large results fill the grid. A connection-only query keeps the data off the sheets and xl reads it by query name, which suits large imports that only Python needs.",[52,279],{"x":54,"y":55,"width":56,"height":280,"fill":58},"207",[52,282],{"x":61,"y":62,"width":283,"height":284,"rx":285,"fill":134,"stroke":98,"style":68},"351.0","175.0","14",[70,287,289],{"transform":288},"translate(182.5,29.0) scale(1.083)",[74,290],{"d":140,"fill":77,"stroke":141,"style":291},"stroke-width:1.66px;stroke-linecap:round;stroke-linejoin:round",[81,293,297],{"x":294,"y":295,"style":296},"195.5","78.0","font-size:14px;font-weight:700;fill:var(--brand-strong,#4338ca);text-anchor:middle","Load as table",[93,299],{"x1":300,"y1":301,"x2":302,"y2":301,"stroke":98,"style":303},"36.0","91.0","355.0","stroke-width:1px",[81,305,309],{"x":306,"y":307,"style":308},"60.0","111.0","font-size:12px;font-weight:400;fill:var(--text,#172033);text-anchor:start","visible and filterable",[70,311,313],{"transform":312},"translate(38.5,99.5) scale(0.625)",[74,314],{"d":315,"fill":77,"stroke":161,"style":316},"M5 12.5l4.5 4.5L19 7","stroke-width:2.88px;stroke-linecap:round;stroke-linejoin:round",[81,318,258],{"x":306,"y":319,"style":308},"137.0",[70,321,323],{"transform":322},"translate(38.5,125.5) scale(0.625)",[74,324],{"d":315,"fill":77,"stroke":161,"style":316},[81,326,328],{"x":306,"y":327,"style":308},"163.0","large imports fill the grid",[70,330,332],{"transform":331},"translate(38.5,151.5) scale(0.625)",[74,333],{"d":334,"fill":77,"stroke":335,"style":316},"M6 6l12 12 M18 6L6 18","var(--accent,#f43f8f)",[52,337],{"x":127,"y":62,"width":283,"height":284,"rx":285,"fill":160,"stroke":161,"style":68},[70,339,341],{"transform":340},"translate(551.5,29.0) scale(1.083)",[74,342],{"d":343,"fill":77,"stroke":168,"style":291},"M10 14a4 4 0 0 0 5.7 0l3-3a4 4 0 0 0-5.7-5.7l-1 1 M14 10a4 4 0 0 0-5.7 0l-3 3a4 4 0 0 0 5.7 5.7l1-1",[81,345,348],{"x":346,"y":295,"style":347},"564.5","font-size:14px;font-weight:700;fill:var(--teal-ink,#0b6157);text-anchor:middle","Connection only",[93,350],{"x1":351,"y1":301,"x2":352,"y2":301,"stroke":161,"style":303},"405.0","724.0",[81,354,356],{"x":355,"y":307,"style":308},"429.0","nothing on the sheets",[70,358,360],{"transform":359},"translate(407.5,99.5) scale(0.625)",[74,361],{"d":315,"fill":77,"stroke":161,"style":316},[81,363,364],{"x":355,"y":319,"style":308},"xl(\"Sales\") by query name",[70,366,368],{"transform":367},"translate(407.5,125.5) scale(0.625)",[74,369],{"d":315,"fill":77,"stroke":161,"style":316},[81,371,372],{"x":355,"y":327,"style":308},"inspect it through Python",[70,374,376],{"transform":375},"translate(407.5,151.5) scale(0.625)",[74,377],{"d":378,"fill":77,"stroke":108,"style":316},"M12 3.5l9.5 17h-19z M12 10v5 M12 17.8v.4",[10,380,381],{},"Choose a table when people need to see the raw rows, a connection when only the analysis matters. For imports of tens of thousands of rows, connection-only keeps the workbook responsive.",[184,383,385],{"id":384},"step-3-read-the-data-in-a-py-cell","Step 3 — Read the data in a PY cell",[10,387,388,389,392],{},"In a cell, type ",[14,390,391],{},"=PY("," and reference the data. For a loaded table:",[221,394,398],{"className":395,"code":396,"language":397,"meta":226,"style":226},"language-python shiki shiki-themes github-light github-dark-high-contrast","sales = xl(\"Sales[#All]\", headers=True)\nsales.dtypes\n","python",[14,399,400,435],{"__ignoreMap":226},[401,402,404,408,412,415,419,422,426,428,432],"span",{"class":93,"line":403},1,[401,405,407],{"class":406},"skGVy","sales ",[401,409,411],{"class":410},"s-kum","=",[401,413,414],{"class":406}," xl(",[401,416,418],{"class":417},"srMev","\"Sales[#All]\"",[401,420,421],{"class":406},", ",[401,423,425],{"class":424},"sa561","headers",[401,427,411],{"class":410},[401,429,431],{"class":430},"sP0c6","True",[401,433,434],{"class":406},")\n",[401,436,438],{"class":93,"line":437},2,[401,439,440],{"class":406},"sales.dtypes\n",[10,442,443],{},"For a connection-only query, reference the query by name — no brackets, no headers argument; the query already has column names:",[221,445,447],{"className":395,"code":446,"language":397,"meta":226,"style":226},"sales = xl(\"Sales\")\nsales.groupby(\"region\")[\"amount\"].sum().sort_values(ascending=False)\n",[14,448,449,462],{"__ignoreMap":226},[401,450,451,453,455,457,460],{"class":93,"line":403},[401,452,407],{"class":406},[401,454,411],{"class":410},[401,456,414],{"class":406},[401,458,459],{"class":417},"\"Sales\"",[401,461,434],{"class":406},[401,463,464,467,470,473,476,479,482,484,487],{"class":93,"line":437},[401,465,466],{"class":406},"sales.groupby(",[401,468,469],{"class":417},"\"region\"",[401,471,472],{"class":406},")[",[401,474,475],{"class":417},"\"amount\"",[401,477,478],{"class":406},"].sum().sort_values(",[401,480,481],{"class":424},"ascending",[401,483,411],{"class":410},[401,485,486],{"class":430},"False",[401,488,434],{"class":406},[10,490,491,492,496,497,500,501,505],{},"Either way you get a pandas DataFrame, and the rest is ordinary pandas. Types set in Power Query arrive as proper types — dates as datetimes, numbers as numbers — which saves the conversion work described in ",[27,493,495],{"href":494},"\u002Fgetting-started-with-python-excel-automation\u002Fusing-python-in-excel\u002Fclean-data-with-pandas-in-python-in-excel\u002F","Clean Data with pandas in Python in Excel",". How ",[14,498,499],{},"xl()"," addresses ranges, tables and names in general is covered in ",[27,502,504],{"href":503},"\u002Fgetting-started-with-python-excel-automation\u002Fusing-python-in-excel\u002Freference-cells-ranges-and-tables-with-xl-in-python-in-excel\u002F","Reference Cells, Ranges and Tables with xl() in Python in Excel",".",[184,507,509],{"id":508},"step-4-refresh-in-the-right-order","Step 4 — Refresh in the right order",[10,511,512,513,516],{},"Power Query and Python recalculate separately. Power Query fetches new data only when refreshed; Python cells recalculate from what is in the workbook. So after the source changes, the sequence is: ",[199,514,515],{},"Data → Refresh All",", which re-runs the queries, and then the Python cells that depend on them recalculate with the new data.",[33,518,42,523,42,526,42,529,42,532,42,535,42,541,42,547,42,550,42,555,42,559,42,562,42,567,42,571,42,574,42,579,42,583,42,588,42,590,42,592,42,594,42,599,42,602,42,610,42,616,42,621,42,624,42,628,42,632,42,637,42,640,42,645,42,650,42,654,42,657,42,662,42,666,42,671,42,674,42,679],{"viewBox":519,"role":36,"ariaLabelledBy":520,"xmlns":40,"style":41},"4 0 752 370",[521,522],"pq-refresh-t","pq-refresh-d",[44,524,525],{"id":521},"What happens on Refresh All",[48,527,528],{"id":522},"The user triggers Refresh All. Power Query fetches new rows from the external source and updates the Sales query in the workbook. Python cells that read the query recalculate in the cloud and return new results to the grid.",[52,530],{"x":54,"y":55,"width":56,"height":531,"fill":58},"370",[52,533],{"x":61,"y":62,"width":534,"height":295,"rx":65,"fill":134,"stroke":98,"style":68},"157.5",[70,536,538],{"transform":537},"translate(86.8,30.5) scale(1.000)",[74,539],{"d":540,"fill":77,"stroke":141,"style":79},"M12 12a4 4 0 1 0 0-8 4 4 0 0 0 0 8z M4 21c0-4 3.6-7 8-7s8 3 8 7",[81,542,546],{"x":543,"y":544,"style":545},"98.8","76.5","font-size:13px;font-weight:700;fill:var(--brand-strong,#4338ca);text-anchor:middle","Excel user",[52,548],{"x":549,"y":62,"width":534,"height":295,"rx":65,"fill":66,"stroke":67,"style":68},"207.5",[70,551,553],{"transform":552},"translate(274.2,30.5) scale(1.000)",[74,554],{"d":114,"fill":77,"stroke":78,"style":79},[81,556,120],{"x":557,"y":544,"style":558},"286.2","font-size:13px;font-weight:700;fill:var(--text,#172033);text-anchor:middle",[52,560],{"x":561,"y":62,"width":534,"height":295,"rx":65,"fill":66,"stroke":67,"style":68},"395.0",[70,563,565],{"transform":564},"translate(461.8,30.5) scale(1.000)",[74,566],{"d":76,"fill":77,"stroke":78,"style":79},[81,568,570],{"x":569,"y":544,"style":558},"473.8","Source",[52,572],{"x":573,"y":62,"width":534,"height":295,"rx":65,"fill":66,"stroke":67,"style":68},"582.5",[70,575,577],{"transform":576},"translate(649.2,30.5) scale(1.000)",[74,578],{"d":167,"fill":77,"stroke":78,"style":79},[81,580,582],{"x":581,"y":544,"style":558},"661.2","Python in Excel",[93,584],{"x1":543,"y1":585,"x2":543,"y2":586,"stroke":67,"style":587},"94.0","354.0","stroke-width:1.5px;stroke-dasharray:5 4",[93,589],{"x1":557,"y1":585,"x2":557,"y2":586,"stroke":67,"style":587},[93,591],{"x1":569,"y1":585,"x2":569,"y2":586,"stroke":67,"style":587},[93,593],{"x1":581,"y1":585,"x2":581,"y2":586,"stroke":67,"style":587},[93,595],{"x1":596,"y1":597,"x2":598,"y2":597,"stroke":98,"style":68},"103.8","134.0","277.2",[100,600],{"points":601,"fill":103},"281.2,134.0 272.2,139.0 272.2,129.0",[52,603],{"x":604,"y":605,"width":606,"height":607,"rx":608,"fill":58,"stroke":67,"style":609},"145.1","105.0","94.9","21.0","10","stroke-width:1.2px",[81,611,615],{"x":612,"y":613,"style":614},"192.5","119.5","font-size:11.5px;font-weight:700;fill:var(--text,#172033);text-anchor:middle","1. Refresh All",[93,617],{"x1":618,"y1":619,"x2":620,"y2":619,"stroke":98,"style":68},"291.2","182.0","464.8",[100,622],{"points":623,"fill":103},"468.8,182.0 459.8,187.0 459.8,177.0",[52,625],{"x":626,"y":63,"width":627,"height":607,"rx":608,"fill":58,"stroke":67,"style":609},"332.1","95.8",[81,629,631],{"x":179,"y":630,"style":614},"167.5","2. fetch rows",[93,633],{"x1":634,"y1":635,"x2":636,"y2":635,"stroke":98,"style":68},"468.8","230.0","295.2",[100,638],{"points":639,"fill":103},"291.2,230.0 300.2,225.0 300.2,235.0",[52,641],{"x":642,"y":643,"width":644,"height":607,"rx":608,"fill":107,"stroke":108,"style":609},"336.7","201.0","86.6",[81,646,649],{"x":179,"y":647,"style":648},"215.5","font-size:11.5px;font-weight:700;fill:var(--gold-ink,#7a4e06);text-anchor:middle","3. new data",[93,651],{"x1":618,"y1":652,"x2":653,"y2":652,"stroke":98,"style":68},"278.0","652.2",[100,655],{"points":656,"fill":103},"656.2,278.0 647.2,283.0 647.2,273.0",[52,658],{"x":659,"y":660,"width":661,"height":607,"rx":608,"fill":58,"stroke":67,"style":609},"415.2","249.0","117.1",[81,663,665],{"x":569,"y":664,"style":614},"263.5","4. Sales updated",[93,667],{"x1":668,"y1":669,"x2":670,"y2":669,"stroke":98,"style":68},"656.2","326.0","107.8",[100,672],{"points":673,"fill":103},"103.8,326.0 112.8,321.0 112.8,331.0",[52,675],{"x":676,"y":677,"width":678,"height":607,"rx":608,"fill":160,"stroke":161,"style":609},"305.1","297.0","149.8",[81,680,683],{"x":179,"y":681,"style":682},"311.5","font-size:11.5px;font-weight:700;fill:var(--teal-ink,#0b6157);text-anchor:middle","5. results recalculated",[10,685,686,687,690],{},"Two settings make this dependable for a shared workbook. In the query's properties, enable ",[204,688,689],{},"Refresh data when opening the file"," so every reader starts from current data. And if the workbook has many Python cells, keep calculation on automatic but avoid editing during a refresh — each changed query triggers the dependent Python cells again.",[184,692,694],{"id":693},"step-5-parameterise-from-a-cell","Step 5 — Parameterise from a cell",[10,696,697],{},"Reports often need a user-chosen value — a region, a start date — applied to the external data. Put the choice in a named cell and read it in Python, filtering the imported data rather than re-querying the source:",[221,699,701],{"className":395,"code":700,"language":397,"meta":226,"style":226},"region = xl(\"SelectedRegion\")\nstart = pd.Timestamp(xl(\"StartDate\"))\nview = sales[(sales[\"region\"] == region) & (sales[\"order_date\"] >= start)]\nview.pivot_table(index=\"rep\", values=\"amount\", aggfunc=\"sum\")\n",[14,702,703,717,733,772],{"__ignoreMap":226},[401,704,705,708,710,712,715],{"class":93,"line":403},[401,706,707],{"class":406},"region ",[401,709,411],{"class":410},[401,711,414],{"class":406},[401,713,714],{"class":417},"\"SelectedRegion\"",[401,716,434],{"class":406},[401,718,719,722,724,727,730],{"class":93,"line":437},[401,720,721],{"class":406},"start ",[401,723,411],{"class":410},[401,725,726],{"class":406}," pd.Timestamp(xl(",[401,728,729],{"class":417},"\"StartDate\"",[401,731,732],{"class":406},"))\n",[401,734,736,739,741,744,746,749,752,755,758,761,764,766,769],{"class":93,"line":735},3,[401,737,738],{"class":406},"view ",[401,740,411],{"class":410},[401,742,743],{"class":406}," sales[(sales[",[401,745,469],{"class":417},[401,747,748],{"class":406},"] ",[401,750,751],{"class":410},"==",[401,753,754],{"class":406}," region) ",[401,756,757],{"class":410},"&",[401,759,760],{"class":406}," (sales[",[401,762,763],{"class":417},"\"order_date\"",[401,765,748],{"class":406},[401,767,768],{"class":410},">=",[401,770,771],{"class":406}," start)]\n",[401,773,775,778,781,783,786,788,791,793,795,797,800,802,805],{"class":93,"line":774},4,[401,776,777],{"class":406},"view.pivot_table(",[401,779,780],{"class":424},"index",[401,782,411],{"class":410},[401,784,785],{"class":417},"\"rep\"",[401,787,421],{"class":406},[401,789,790],{"class":424},"values",[401,792,411],{"class":410},[401,794,475],{"class":417},[401,796,421],{"class":406},[401,798,799],{"class":424},"aggfunc",[401,801,411],{"class":410},[401,803,804],{"class":417},"\"sum\"",[401,806,434],{"class":406},[10,808,809],{},"Filtering in Python is right when the import is already small. When the choice should reduce what is fetched — a year of transactions instead of ten — make it a Power Query parameter instead, so the filter runs at the source and less data crosses into the workbook at all.",[184,811,813],{"id":812},"step-6-combine-a-folder-of-files","Step 6 — Combine a folder of files",[10,815,816,817,820,821,824],{},"Monthly exports dropped into one folder are a common source, and Power Query's ",[204,818,819],{},"From Folder"," connector combines them into one query: it lists the files, applies the same import steps to each, and appends the results with a column holding the source file name. Filter the file list in the editor — by extension, or by a name pattern such as ",[14,822,823],{},"sales_2026_*"," — so stray files in the folder do not break the combine step. Load the combined query as connection only and analyse it in Python:",[221,826,828],{"className":395,"code":827,"language":397,"meta":226,"style":226},"combined = xl(\"MonthlySales\")\nper_file = combined.groupby(\"Source.Name\")[\"amount\"].agg([\"count\", \"sum\"])\nper_file\n",[14,829,830,844,874],{"__ignoreMap":226},[401,831,832,835,837,839,842],{"class":93,"line":403},[401,833,834],{"class":406},"combined ",[401,836,411],{"class":410},[401,838,414],{"class":406},[401,840,841],{"class":417},"\"MonthlySales\"",[401,843,434],{"class":406},[401,845,846,849,851,854,857,859,861,864,867,869,871],{"class":93,"line":437},[401,847,848],{"class":406},"per_file ",[401,850,411],{"class":410},[401,852,853],{"class":406}," combined.groupby(",[401,855,856],{"class":417},"\"Source.Name\"",[401,858,472],{"class":406},[401,860,475],{"class":417},[401,862,863],{"class":406},"].agg([",[401,865,866],{"class":417},"\"count\"",[401,868,421],{"class":406},[401,870,804],{"class":417},[401,872,873],{"class":406},"])\n",[401,875,876],{"class":93,"line":735},[401,877,878],{"class":406},"per_file\n",[10,880,881,882,505],{},"The per-file summary doubles as a completeness check: a month with far fewer rows than the others, or a file that is missing altogether, shows up immediately. Adding next month's file to the folder and refreshing is all the maintenance the report needs. Outside Excel, the same job is done with a loop over the folder — see ",[27,883,885],{"href":884},"\u002Fgetting-started-with-python-excel-automation\u002Fworking-with-multiple-excel-sheets-in-python\u002Fcombine-multiple-excel-files-into-one-python\u002F","Combine Multiple Excel Files into One in Python",[184,887,889],{"id":888},"common-pitfalls","Common pitfalls",[247,891,892,905,911,917],{},[250,893,894,897,898,421,901,904],{},[199,895,896],{},"Trying to read files or URLs from Python."," ",[14,899,900],{},"open()",[14,902,903],{},"pd.read_csv()"," on a local path and web requests fail in Python in Excel. Route every external source through Power Query.",[250,906,907,910],{},[199,908,909],{},"Forgetting to refresh."," Python recalculates from stale imported data until the query is refreshed. Refresh All first, or refresh on open.",[250,912,913,916],{},[199,914,915],{},"Loading everything."," A PY cell sends what it reads to the cloud on each recalculation. Filter and aggregate in Power Query so Python gets only what the analysis needs.",[250,918,919,922],{},[199,920,921],{},"Credentials for other readers."," A query that works for you may prompt a colleague for credentials or be blocked by privacy levels. Test the workbook as a reader would before sharing it.",[184,924,926],{"id":925},"performance-and-scale-notes","Performance and scale notes",[10,928,929,930,934,935,505],{},"Power Query is efficient at import and filtering, and for databases it can push filters down to the server. Python in Excel is efficient at analysis on moderate data. The combination works best when Power Query does the reduction and Python the reasoning: tens of thousands of rows reaching Python is comfortable, millions are not. When a job needs full-size data, scheduled refreshes or files on a server, a regular Python script outside Excel is the better tool — the trade-off is laid out in ",[27,931,933],{"href":932},"\u002Fgetting-started-with-python-excel-automation\u002Fusing-python-in-excel\u002Fpython-in-excel-vs-openpyxl-and-xlwings\u002F","Python in Excel vs openpyxl and xlwings",", and database loading from scripts in ",[27,936,938],{"href":937},"\u002Fadvanced-data-transformation-and-cleaning\u002Fmoving-data-between-excel-and-databases\u002F","Moving Data Between Excel and Databases",[184,940,942],{"id":941},"conclusion","Conclusion",[10,944,945,946,948],{},"Python in Excel cannot reach files or the internet, so external data comes in through Power Query. Import and shape the source there, load it as a table when people need to see it or as a connection-only query when only Python does, read it with ",[14,947,499],{}," by table or query name, and refresh the queries before relying on the Python results. Keep heavy filtering in Power Query so Python works on the rows that matter.",[184,950,952],{"id":951},"frequently-asked-questions","Frequently asked questions",[10,954,955,958],{},[199,956,957],{},"Can Python in Excel read a CSV file from my computer?","\nNo. Python in Excel runs in an isolated cloud environment with no access to your file system or the internet. Import the file with Power Query first, then read the resulting table or query with xl().",[10,960,961,964],{},[199,962,963],{},"Do I have to load the Power Query result onto a sheet?","\nNo. A query saved as connection only can be referenced by name from a PY cell with xl(\"QueryName\"), which keeps large imports off the grid.",[10,966,967,970],{},[199,968,969],{},"Why does my Python cell show old data after the source changed?","\nThe Python cell recalculates from what is in the workbook. Refresh the Power Query query first — Data, Refresh All — and the Python cells that depend on it recalculate afterwards.",[10,972,973,976],{},[199,974,975],{},"How much data can I bring in this way?","\nPower Query itself handles large imports, but everything a PY cell reads is sent to the cloud on each recalculation. Filter and aggregate in Power Query so Python receives the rows it needs, not the whole source.",[184,978,980],{"id":979},"related","Related",[247,982,983,989,996,1003],{},[250,984,985,986,988],{},"Up one level: ",[27,987,30],{"href":29}," — how =PY() works.",[250,990,991,995],{},[27,992,994],{"href":993},"\u002Fgetting-started-with-python-excel-automation\u002Fusing-python-in-excel\u002Freturn-dataframes-and-values-from-python-in-excel-cells\u002F","Return DataFrames and Values from Python in Excel Cells"," — getting results back to the grid.",[250,997,998,1002],{},[27,999,1001],{"href":1000},"\u002Fgetting-started-with-python-excel-automation\u002Fusing-python-in-excel\u002Ffix-python-in-excel-errors\u002F","Fix Python in Excel Errors"," — including access errors.",[250,1004,1005,1009],{},[27,1006,1008],{"href":1007},"\u002Fgetting-started-with-python-excel-automation\u002Fchoosing-a-python-excel-library\u002Fpython-vs-power-query-for-cleaning-excel-data\u002F","Python vs Power Query for Cleaning Excel Data"," — which tool for which job.",[1011,1012,1013],"style",{},"html pre.shiki code .skGVy, html code.shiki .skGVy{--shiki-default:#24292E;--shiki-dark:#F0F3F6}html pre.shiki code .s-kum, html code.shiki .s-kum{--shiki-default:#D73A49;--shiki-dark:#FF9492}html pre.shiki code .srMev, html code.shiki .srMev{--shiki-default:#032F62;--shiki-dark:#ADDCFF}html pre.shiki code .sa561, html code.shiki .sa561{--shiki-default:#E36209;--shiki-dark:#FFB757}html pre.shiki code .sP0c6, html code.shiki .sP0c6{--shiki-default:#005CC5;--shiki-dark:#91CBFF}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}",{"title":226,"searchDepth":437,"depth":437,"links":1015},[1016,1017,1018,1019,1020,1021,1022,1023,1024,1025,1026,1027],{"id":186,"depth":437,"text":187},{"id":193,"depth":437,"text":194},{"id":238,"depth":437,"text":239},{"id":384,"depth":437,"text":385},{"id":508,"depth":437,"text":509},{"id":693,"depth":437,"text":694},{"id":812,"depth":437,"text":813},{"id":888,"depth":437,"text":889},{"id":925,"depth":437,"text":926},{"id":941,"depth":437,"text":942},{"id":951,"depth":437,"text":952},{"id":979,"depth":437,"text":980},"2026-09-23","Python in Excel cannot read files or call the internet, so bring CSVs, databases and web data in with Power Query, then analyse them with xl() — tables versus connection-only queries, refresh order, parameters and size limits.","md",[1032,1034,1036,1038],{"q":957,"a":1033},"No. Python in Excel runs in an isolated cloud environment with no access to your file system or the internet. Import the file with Power Query first, then read the resulting table or query with xl().",{"q":963,"a":1035},"No. A query saved as connection only can be referenced by name from a PY cell with xl(\"QueryName\"), which keeps large imports off the grid.",{"q":969,"a":1037},"The Python cell recalculates from what is in the workbook. Refresh the Power Query query first — Data, Refresh All — and the Python cells that depend on it recalculate afterwards.",{"q":975,"a":1039},"Power Query itself handles large imports, but everything a PY cell reads is sent to the cloud on each recalculation. Filter and aggregate in Power Query so Python receives the rows it needs, not the whole source.","get external data into python in excel with power query",null,{"breadcrumb":1043},[1044,1047,1050],{"name":1045,"item":1046},"Home","\u002F",{"name":1048,"item":1049},"Getting Started with Python Excel Automation","\u002Fgetting-started-with-python-excel-automation\u002F",{"name":30,"item":29},true,"\u002Fgetting-started-with-python-excel-automation\u002Fusing-python-in-excel\u002Fget-external-data-into-python-in-excel",{"title":1054,"description":1055},"Load External Data into Python in Excel with Power Query","Python in Excel has no file or network access. Import CSV, folder, database and web data with Power Query, reference the result with xl() as a table or connection-only query, and refresh reliably.","get-external-data-into-python-in-excel","getting-started-with-python-excel-automation\u002Fusing-python-in-excel\u002Fget-external-data-into-python-in-excel\u002Findex",[1059,1060],"python-in-excel","data-sources","how-to","bjq7DaBMJpoOE53BbpEYpLChSuiVRCFqs1JudKgURGQ",[1064,1068],{"title":1065,"path":1066,"stem":1067,"children":-1},"Forecast a Time Series with Python in Excel","\u002Fgetting-started-with-python-excel-automation\u002Fusing-python-in-excel\u002Fforecast-a-time-series-with-python-in-excel","getting-started-with-python-excel-automation\u002Fusing-python-in-excel\u002Fforecast-a-time-series-with-python-in-excel\u002Findex",{"title":1069,"path":1070,"stem":1071,"children":-1},"Plot Charts with matplotlib in Python in Excel","\u002Fgetting-started-with-python-excel-automation\u002Fusing-python-in-excel\u002Fplot-charts-with-matplotlib-in-python-in-excel","getting-started-with-python-excel-automation\u002Fusing-python-in-excel\u002Fplot-charts-with-matplotlib-in-python-in-excel\u002Findex",[1073,1075],{"path":1074,"title":1048},"\u002Fgetting-started-with-python-excel-automation",{"path":1076,"title":30},"\u002Fgetting-started-with-python-excel-automation\u002Fusing-python-in-excel",[],[1079,1081,1084,1085,1086,1089],{"path":1080,"title":495,"kind":1061},"\u002Fgetting-started-with-python-excel-automation\u002Fusing-python-in-excel\u002Fclean-data-with-pandas-in-python-in-excel",{"path":1082,"title":1001,"kind":1083},"\u002Fgetting-started-with-python-excel-automation\u002Fusing-python-in-excel\u002Ffix-python-in-excel-errors","fix",{"path":1066,"title":1065,"kind":1061},{"path":1070,"title":1069,"kind":1061},{"path":1087,"title":933,"kind":1088},"\u002Fgetting-started-with-python-excel-automation\u002Fusing-python-in-excel\u002Fpython-in-excel-vs-openpyxl-and-xlwings","compare",{"path":1090,"title":504,"kind":1061},"\u002Fgetting-started-with-python-excel-automation\u002Fusing-python-in-excel\u002Freference-cells-ranges-and-tables-with-xl-in-python-in-excel",1790972075251]