我想帮助我的会计同事,他们从门户网站下载多个以一堆数字命名的.pdf文件,然后按照如下格式手动重命名:
名称_发票编号_发票日期
他们每周都手动做这件事,发票量很大,浪费时间。
我发现门户可以批量下载 .pdf 和 .xml 格式的发票,这两个文件都使用相同的数字 ID 命名。
我如何编写一个代码,使用上面的格式根据镜面 xml 数据批量重命名每个发票 .pdf?
我主要使用Python用于网络目的,所以我真的不知道如何解决这个问题。 非常感谢您提前。
编辑:
澄清一下,下载的文件如下所示。
https://i.ibb.co/SRfCdxC/File-ID-Example.png
相关行中的 .XML 是:
<ns3:FatturaElettronica xmlns:ns2="http://www.#" xmlns:ns3="http://" versione="FPR12">
<FatturaElettronicaHeader>
<CedentePrestatore>
<DatiAnagrafici>
<Anagrafica>
<!-- The Name of the invoice sender that i need -->
<Denominazione>Jason Smith</Denominazione>
</Anagrafica>
</DatiAnagrafici>
</CedentePrestatore>
</FatturaElettronicaHeader>
<FatturaElettronicaBody>
<DatiGenerali>
<DatiGeneraliDocumento>
<!-- The date of the Invoice -->
<Data>Invoice_Date_2024-03-12</Data>
<!-- Invoice Number -->
<Numero>Invoice_Number 000000000000</Numero>
</DatiGeneraliDocumento>
</DatiGenerali>
</FatturaElettronicaBody>
</ns3:FatturaElettronica>
在上面的示例中,重命名的文件应如下所示:
Jason Smith - Invoice_000000000000 - of 2024-03-12
这是一个 powershell 脚本,它将重命名该文件
using assembly System.Xml.Linq
$folder = 'c:\temp\test'
$xmlFiles = Get-ChildItem -Path $folder\*.xml.p7m
foreach($xmlFile in $xmlFiles)
{
$doc = [System.Xml.Linq.XDocument]::Load($xmlFile)
$dominazione = $doc.Descendants('Denominazione')[0].Value
$data = $doc.Descendants('Data')[0].Value
$data = $data.Replace('Invoice_Date_','')
$numero = $doc.Descendants('Numero')[0].Value
$numero = $numero.Replace('Number ','')
$newFilename = [string]::Format('{0} - {1} - of {2}',$dominazione,$numero,$data)
$periodIndex = $xmlFile.Name.IndexOf('.')
$pdfFilename = $xmlFile.Name.Substring(0, $periodIndex)
Rename-Item -Path ($folder + '\' + $pdfFilename) ($folder + '\' + $newFilename)
}